We are migrating our application from eclipselink to hibernate. In our legacy code we are persisting the object first, then setting the values. This used to work in eclipselink but fails in hibernate in the case when we try to call em.persist before setting the value of any field which has not-null constraint in DB.
Error message I get:
could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
eclipselink code which works:
//eclipselink 2.5.1
@javax.ejb.TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRED)
public void save(Person p){
em.persist(p);
p.setEmail("abc@xyz.com");
}
Hibernate code which doesnt work(same code as eclipselink)
//hibernate 5.6.15
@org.springframework.transaction.annotation.Transactional
public void save(Person p){
em.persist(p);
p.setEmail("abc@xyz.com");
}
Here p.setEmail() is set as not-null in DB. And exception is thrown after the method execution is completed and it goes to commit the transaction. And in logs the generated insert query has null value for property 'email'.