How to disable updates for a field of an Entity EJB?
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.
Recommended Articles
Lookup EJBs in JAR Files from Web Applications Using @EJB Annotation and Deployment Descriptors
Learn how to look up an EJB inside a JAR file from a Web application using the @EJB annotation and deployment descriptors. #EnterpriseJava #WildFly #Jboss #EJB2
Mastering Bean Managed Transactions (BMT) in Java EE 3 Applications
Learn how to use Bean Managed Transactions (BMT) in EJB 3 applications for better transaction control and flexibility.
Exposing an Enterprise Java Bean as a Singleton: A Simple Approach
Learn how to use the @Singleton annotation to expose an Enterprise Java Bean as a single instance, ensuring thread-safe and efficient management of your application's resources.
Configure Custom Pool Settings for EJBs in JBoss AS 7/WildFly
Learn how to customize pool settings for your EJBs on JBoss AS 7/WildFly, including configuring a custom pool and applying specific settings using the @Pool annotation.