I am using web-services with spring-boot 2.0.6 and hibernate 5.2.17. When I save an object with a createTime field of @CreationTimestamp, @Column(nullable = false) and @Temporal(TemporalType.TIMESTAMP), I get the exception org.hibernate.PropertyValueException: not-null property references a null or transient value : com.ex.Entity.createTime. But in another service (spring-boot 1.5.6 and hibernate 5.0.12), this problem did not occur.
So I debugged it step by step through debugging, and finally found that when hibernate checked the field with @Column(nullable=false)
org.hibernate.engine.internal.Nullability#checkNullability(Object[] values, EntityPersister persister, boolean isUpdate)
the variable Nullability#checkNullability is true in the high version and the low version is false.
@Table(name = "t_entity")
@Entity
class Entity{
@Id
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@GeneratedValue(generator = "system-uuid")
@BeanProperty
var id: String = _
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false, updatable = false)
@BeanProperty
var createTime: Date = _
//other fields are ignored
}
trait EntityRepository extends JpaRepository[Entity, String]
with QuerydslPredicateExecutor[Entity] {
}
I know that changing the version (I can't) or setting the value of createTime (stupid) can solve this problem, and I think java and scala have little effect on this issue, so the example code is scala. Is there a better way to solve it like an annotation?