乐观锁定:需要更新的两个字段

时间:2021-08-27 00:47:20

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.

现在我使用Eclipselink和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".

我设法通过使用@Version注释来增加版本。但是,只有在名称字段确实发生变化时,我才能更新已更改的字段。只需每次设置更改的字段,每次都会使JPA更新行。即使名称字段没有真正的变化。而且我不想检查名称字段是否真的被“手”改变了。

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

我是否也可以将@Version注释设置为第二个字段?

1 个解决方案

#1


0  

I found it:

我找到了:

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

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

它仅在EntityManager决定进行更新时调用。

#1


0  

I found it:

我找到了:

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

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

它仅在EntityManager决定进行更新时调用。