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: {code} 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) ); {code} On creation of a TestEntity the expected SQL is created: {noformat} Hibernate: insert into TestEntity (createdDate, lastModifiedDate, value, version) values (?, ?, ?, ?) {noformat} On update of the TestEntity, by modifying just the value data member, the update statement is also as expected: {noformat} update TestEntity set lastModifiedDate=?, value=?, version=? where id=? and version=? {noformat} 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: {noformat} insert into TestEntity (createdDate, lastModifiedDate, value, version) values (?, ?, ?, ?) {noformat} Hibernate: {noformat} update TestEntity set value=?, version=? where id=? and version=? {noformat} 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. |
|