Ben Guenter
Ben Guenter

Reputation: 208

Can Hibernate update a single property?

I have a hibernate class with more than one property, I have heard that NHibernate tracks properties in files and only updates those properties that have changed. Is there similar functionality in Hibernate?

I have tried to get it to work using just a simple class load - then set one property - then flush changes. However this without fail updates all the properties in the class.

Upvotes: 3

Views: 3822

Answers (3)

Panos K.
Panos K.

Reputation: 61

You can use the @DynamicUpdate annotation at the entity level, as shown here.

Upvotes: 0

Seymur Asadov
Seymur Asadov

Reputation: 672

You can get Entity from db first then update ... try this example..

public final void updateEntity(EntityName entity){

  //get entity from db by id
  EntityName dbEntity = session.get(EntityName.class, item.getProductId());

   //set property
  dbEntity.setStatus(entity.getStatus());

  //and update
  session.update(dbEntity);

}

Upvotes: 0

Piyush Mattoo
Piyush Mattoo

Reputation: 16115

You need to set the dynamic-update property to true via annotation or in the class mapping which would exclude unmodified properties in the Hibernate’s SQL update statement. You can find a good reference here. By default, the dynamic-update is set to false so as to be backward compatible as it is somewhat new feature introduced.

Upvotes: 4

Related Questions