Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is insertable and updatable in JPA?

The accepted answer conveys the feeling that insertable/updatable attribute has to do with the creation/update of the related entity, whereas the real intention behind these attributes is to prevent insertion/update of the column in the current entity.

What is @column in JPA?

In this article, we will discuss how to change the column name in the Spring project using JPA. @Column annotation is used for Adding the column the name in the table of a particular MySQL database. Syntax: @Column(name=”DESC”, nullable=false, length=512)

Is @column annotation necessary?

Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

What is @basic annotation in JPA?

We can use the @Basic annotation to mark a basic type property: @Entity public class Course { @Basic @Id private int id; @Basic private String name; ... } In other words, the @Basic annotation on a field or a property signifies that it's a basic type and Hibernate should use the standard mapping for its persistence.


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


Defining insertable=false, updatable=false is useful when you need to map a field more than once in an entity, typically:

  • when using a composite key
  • when using a shared primary key
  • when using cascaded primary keys

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.


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


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.


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.