If the entity persisted in transaction is further modified, then the @CreationTimestamp field will become null. Are there any solutions to this problem? Magic hibernate flags? Or do we only have to create the field manually in the @PrePersist method? I also don’t want to flush the data after the persist method, as this is an unnecessary database call.
@Entity
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private String uuid;
@CreationTimestamp
private Instant created;
private String userID;
public void setUserID(String userID) {
this.userID = userID;
}
}
public class TestResource {
@Inject
EntityManager entityManager;
@Transactional
public void test() {
MyEntity myEntity = new MyEntity();
entityManager.persist(myEntity);
// If this line is executed, the value of "created" will be set to null.
myEntity.setUserID("userID");
}
}
insert into MyEntity (created, userID, uuid) values (?, ?, ?)
update MyEntity set created=?, userID=? where uuid=?
created=? - i'm 99% sure 'null' here
1