简体   繁体   English

仅更新非空字段

[英]Update only not null fields

How to tell hibernate to not update fields with null values ? 如何告诉休眠不更新具有空值的字段?

I have filled entity (without password field which is not-null field). 我已经填写了实体(没有密码字段,该字段不是null)。 I want to update all filled fields in entity (skip the fields with null values) but I recive error like that: 我想更新实体中的所有填充字段(跳过具有空值的字段),但我收到如下错误:

by: org.postgresql.util.PSQLException: ERROR: null value in column "password" violates not-null constraint

How to tell hibernate to update ONLY changed fields (an skip all null values). 如何告诉休眠仅更新更改的字段(跳过所有空值)。

Edit (code) 编辑(代码)

workerService: workerService:

public Worker updateWorker(Worker worker) { // only a few fields are filled.
     workerDao.update(worker);
     return worker;
}

and DAO: 和DAO:

@Repository
@Transactional
abstract public class AbstractDao<T> {

@Autowired
private SessionFactory sessionFactory;

//....

public void update(T entity) {
    sessionFactory.getCurrentSession().update(entity);
}
//...

WorkerEntity: WorkerEntity:

@Entity
@Table(name = "worker", schema = "public")
@DynamicUpdate
public class Worker implements java.io.Serializable {

The dynamic-update attribute tells Hibernate whether to include unmodified properties in the SQL UPDATE statement. dynamic-update属性告诉Hibernate是否在SQL UPDATE语句中包括未修改的属性。

dynamic-update=true then only updated column will appear in update query dynamic-update = true,那么更新查询中将仅显示已更新的列

@Entity
@Table(name = "test", catalog = "test_1")
@org.hibernate.annotations.Entity(
        dynamicUpdate = true
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM