如何告诉hibernate不用空值更新字段?
我填充了实体(没有密码字段,这是非空字段)。我想更新实体中的所有填充字段(跳过具有空值的字段),但我发现错误:
by: org.postgresql.util.PSQLException: ERROR: null value in column "password" violates not-null constraint
如何告诉hibernate仅更新已更改的字段(跳过所有空值)。
workerService:
public Worker updateWorker(Worker worker) { // only a few fields are filled.
workerDao.update(worker);
return worker;
}
和DAO:
@Repository
@Transactional
abstract public class AbstractDao<T> {
@Autowired
private SessionFactory sessionFactory;
//....
public void update(T entity) {
sessionFactory.getCurrentSession().update(entity);
}
//...
WorkerEntity:
@Entity
@Table(name = "worker", schema = "public")
@DynamicUpdate
public class Worker implements java.io.Serializable {
答案 0 :(得分:0)
dynamic-update属性告诉Hibernate是否在SQL UPDATE语句中包含未修改的属性。
dynamic-update = true然后只有更新的列才会出现在更新查询
中@Entity
@Table(name = "test", catalog = "test_1")
@org.hibernate.annotations.Entity(
dynamicUpdate = true
)