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

179,466

Solution 1

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

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

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

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

Solution 5

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.

Share:
179,466

Related videos on Youtube

Thang Pham
Author by

Thang Pham

Updated on June 25, 2021

Comments

  • Thang Pham
    Thang Pham almost 3 years

    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;
    }
    
  • Jayant
    Jayant almost 9 years
    I strongly believe that this answer is much better that the accepted one. 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. The creation/update of the related entity is tackled by the cascade attribute of the mapping annotation.
  • eis
    eis over 8 years
    sequences are supported by JPA as well, so you could define your sequence with JPA annotations, too.
  • Flowy
    Flowy over 7 years
    You are saying that with updatable=false on Person it will disable updating of Person.name when updating address (I disagree as this is the purpose of cascade). Also u are saying that @Column definition does something different when its foreign key (Person) and when it is not foreign key (as there is no referenced entity to disable updating). By reading javadoc for updatable I would say that it will just disable to change Person for given Address if it is once persisted. Can you explain please?
  • chrisinmtown
    chrisinmtown about 6 years
    Is Hibernate supposed to block updates based on an updatable=false annotation? In my JPA repository test a created_on column with this annotation accepts updates without complaint.
  • Jaqen H'ghar
    Jaqen H'ghar over 5 years
    @chrisinmtown Eclipselink will not include the column in the sql at all. I expect it is the same with Hibernate
  • Daniel Pop
    Daniel Pop over 3 years
    actually this is the response I came for
  • Zyl
    Zyl over 3 years
    Isn't this sufficiently defined by the cascade options already? I saw this being used to not run into trouble by trying to write to a generated column. (e.g. a volume column GENERATED ALWAYS AS (height * width * length) VIRTUAL)
  • GlauberMD
    GlauberMD almost 2 years
    It helped me understand why a column annotated with @CreatedDate and/or @UpdateTimestamp was not getting updated if it also has @Column(...insertable=false, updatable=false). Thanks!