|
|
|
|
|
|
I'm using the org.hibernate.annotations.CreationTimestamp and UpdateTimestamp annotations as examples, heading towards similar annotations that use a in-database value using current_timestamp. As the first step I want to show that @CreationTimestamp, @UpdateTimestamp and @DynamicUpdate annotations work together.
As my starting point In TestEntity.java (attached) I've added @CreationTimestamp to the createdDate data member and @UpdateTimestamp to the lastModifiedDate data member.
The database table is: CREATE TABLE TestEntity ( id bigserial NOT NULL, version bigint NOT NULL, createdDate timestamp(6) NOT NULL, lastModifiedDate timestamp(6) NOT NULL, value int NOT NULL, CONSTRAINT PK_TestEntity PRIMARY KEY (id) );
On creation of a TestEntity the expected SQL is created:
Hibernate: insert into TestEntity (createdDate, lastModifiedDate, value, version) values (?, ?, ?, ?)
On update of the TestEntity , by modifying just the value data member, the update statement is also as expected: update TestEntity set lastModifiedDate=?, value=?, version=? where id=? and version=?
Note that I seem to need to define @Column(insertable=true, updatable=false) annotation for the createdDate data member otherwise it is rewritten on updates to the entity (not sure if this is expected or not).
If I then uncomment the @DynamicUpdate in TestEntity.java, recompile and rerun the test I get the following SQL generated:
Hibernate: insert into TestEntity (createdDate, lastModifiedDate, value, version) values (?, ?, ?, ?)
Hibernate: update TestEntity set value=?, version=? where id=? and version=?
So it looks like @DynamicUpdate doesn't detect that the lastModifiedDate data member is a generated value and should be recorded in the database when the row is being updated in the database.
I run into additional problems when I replace the @CreationTimestamp and @UpdateTimestamp annotations with custom ones that generate the value in the database by using current_timestamp (which is my ultimate goal) but I will post them separately after I have confirmation that my understanding of this first problem is correct.
|
|
|
|
|
|