Hibernate : Make a field Non Updatable in an Entity

Points To Remember

By default all the fields in the Hibernate Entity are updatable and insertable. To make a field non updatable in hibernate you nee to add the annotation @Column(updatable = false)

Make a field Non Updatable in an Hibernate Entity

Suppose you have a User class and you want to make email as a non updatable field, all you need to do is add the @Column(updatable = false) annotation as shown in the example below.

public class User {

    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "email",updatable = false, nullable = false, unique = true)
    private String email;


Note : This only puts a hibernate level constraint, however you can still manually go and change the value of email by using db level queries.

No comments :

Post a Comment