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.
精彩评论