How to update only a part of all entity attributes with Hibernate

If you never want to update those two fields, you can mark them with @Column(updatable=false):

@Column(name="CREATED_ON", updatable=false)
private Date createdOn;

Once you load an entity and you modify it, as long as the current Session or EntityManager is open, Hibernate can track changes through the dirty checking mechanism. Then, during flush, an SQL update will be executed.

If you don't like that all columns are included in the UPDATE statement, you can use a dynamic update:

@Entity
@DynamicUpdate
public class Product {
   //code omitted for brevity
}

Then, only the modified columns will be included in the UPDATE statement.


Do like this example :

    public class UpdateMethodDemo {
        public static void main(String[] args) {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Student s = studentService.getById(1);
            s.setNom("FuSsA");
            session.beginTransaction();
            session.update(s);
            session.getTransaction().commit();
            session.close();
        }

} 

Edit:

you can use @Transient annotation to indicate that a field is not to be persisted in the database.