简体   繁体   中英

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). 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:

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

and 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 {

The dynamic-update attribute tells Hibernate whether to include unmodified properties in the SQL UPDATE statement.

dynamic-update=true then only updated column will appear in update query

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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