开发者

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 jus开发者_运维知识库t a simple class load - then set one property - then flush changes. However this without fail updates all the properties in the class.


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.


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);

}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜