If a field is annotated insertable=false, updatable=false
, doesn't it mean that you cannot insert value nor change the existing value? Why would you want to do that?
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(mappedBy="person", cascade=CascadeType.ALL)
private List<Address> addresses;
}
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name="ADDRESS_FK")
@Column(insertable=false, updatable=false)
private Person person;
}
This question is related to
java
jpa
jakarta-ee
eclipselink
Defining insertable=false, updatable=false
is useful when you need to map a field more than once in an entity, typically:
This is IMO not a semantical thing, but definitely a technical one.
I would like to add to the answers of BalusC and Pascal Thivent another common use of insertable=false, updatable=false
:
Consider a column that is not an id but some kind of sequence number. The responsibility for calculating the sequence number may not necessarily belong to the application.
For example, sequence number starts with 1000 and should increment by one for each new entity. This is easily done, and very appropriately so, in the database, and in such cases these configurations makes sense.
According to Javax's persistence documentation:
Whether the column is included in SQL UPDATE statements generated by the persistence provider.
It would be best to understand from the official documentation here.
An other example would be on the "created_on" column where you want to let the database handle the date creation
Source: Stackoverflow.com