If you need to select which columns need update when executing a CMP statement you can use the “updatable” attribute on the @Column annotation.
Setting “updatable” = false will assume that the column is not always included in the SQL update statement.
Here is an example:
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; @Entity public class Model { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "model_generator") @SequenceGenerator(name = "model_generator", sequenceName = "model_seq") @Column private Long id; @Column private String name; @Column(updatable = false) private String avatar; }
Important notice: as you can read from the javadoc of Hibernate 5.4 the updatable attribute is marked as optional:
(Optional) Whether the column is included in SQL UPDATE statements generated by the persistence provider.
As a matter of fact, if you issue an update statement with HQL or using CriteriaUpdate, against a field annotated with @Column(updatable = false)
, your update statement will be executed .
The @Column(updatable = false)
will skip the field update if you are using either Hibernate’s update
or JPA’s merge
method.