2

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?

xmcx
  • 283
  • 3
  • 18

1 Answers1

0

I had exactly the same issue, it helped when I added testImplementation("org.springframework.boot:spring-boot-starter-web") dependency into my project. You might want it in test or compile scope.

zjor
  • 994
  • 2
  • 12
  • 22
  • Which module(domain, facade, repository, biz) should I add to it? – xmcx Nov 07 '19 at 09:28
  • It's a dependency in `build.gradle` file. I added this dep to the module has executable code and uses this domain class. – zjor Nov 08 '19 at 10:04