Please explain about insertable=false and updatable=false in reference to the JPA @Column annotation

JavaJpaJakarta EeEclipselink

Java Problem Overview


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;
}

Java Solutions


Solution 1 - Java

You would do that when the responsibility of creating/updating the referenced column isn't in the current entity, but in another entity.

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

An other example would be on the "created_on" column where you want to let the database handle the date creation

Solution 5 - Java

Adding to the previous answers, a common use of insertable=false, updatable=false is to save redundant database queries, thereby improving performance.

Imagine having a Client class, which has a Parent entity. If you just want to check if a Client has a Parent, you simply need to check for the presence of value in its parent_id column. There is no need to ask Hibernate to fetch the Parent entity, with possibly all of its other associations, leading to additional number of queries:

public class Client {
    @ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private Parent parent;

    @Column(name = "parent_id", insertable = false, updatable = false)
    private UUID parentId;
}

With the setup above, the parentId field will simply fetch whatever value is stored in the parent_id column, which is solely edited/updated by the Parent entity.

Solution 6 - Java

I think it simply mean :

Whether the column is included in SQL INSERT statements generated by the persistence provider.

Reference: https://www.objectdb.com/api/java/jpa/Column

Solution 7 - Java

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.

Solution 8 - Java

Another reason could be that your attribute is mapper to a column of a view (Example, your hibernate entity is the fusion of a table and a view). So it does not make sens that your column can be inserted (nor updated).

@Entity
@Table(name = "THE_VIEW")
@SecondaryTable(name = "THE_TABLE", pkJoinColumns = @PrimaryKeyJoinColumn(name = "THE_ID"))
public class MyEntity {

	@Id
	@Column(name = "THE_ID")
	private Integer id;

	@Column(name = "VIEW_COLUMN", updatable = false, insertable = false)
	private String viewColumn

    @Column(name = "TABLE_COLUMN", table = "THE_TABLE")
    private String tableColumn;

(I don't talk about updatable views here)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThang PhamView Question on Stackoverflow
Solution 1 - JavaBalusCView Answer on Stackoverflow
Solution 2 - JavaPascal ThiventView Answer on Stackoverflow
Solution 3 - JavaMagnilexView Answer on Stackoverflow
Solution 4 - JavaJohny19View Answer on Stackoverflow
Solution 5 - JavaPetar BivolarskiView Answer on Stackoverflow
Solution 6 - JavaJoshView Answer on Stackoverflow
Solution 7 - JavaNimitView Answer on Stackoverflow
Solution 8 - JavaihebihebView Answer on Stackoverflow