0
votes

In our old db strukture we have two fields in one table that get updated when we change data.

create table dbo.example(
   name varchar(50),
   ...,
   changed smalldatetime,   -- here we save the last update date
   version int              -- and here we increase the number
)

Don't ask. It is like it is :-)

Now I implement optimistic locking with Eclipselink and Glassfish v3.

I managed to increase the version by using the @Version annotation. But how can i update the changed field only when the name field really changes. Just setting the changed field every time make the JPA update the row every time. Even if there was no real change to the name field. And I don't want to check if the name field has really changed by "hand".

Can I also set the @Version annotation to the second field too?

1

1 Answers

0
votes

I found it:

@PreUpdate
private void update() {
    changed = new Date();
}

It's only called when EntityManager decides to do an update.