[英]SEAM/Hibernate: I can't get changes from database during a Conversation
I have a class: 我有一堂课:
@Name("foo")
@Scope(ScopeType.CONVERSATION)
public class Foo {
@In
Session session;
@In
private Conversation conversation;
@RequestParameter
Long barId;
/* outjected fields */
@Out
Bar bar;
/* some attributes and methods */
public void start() {
/* some initializations */
this.bar = (Bar) session.get(Bar.class, barId);
}
public void end() {
/* some actions involving bar and bar.getBaz() */
conversation.end();
}
}
The method start() is called at the beginning of the conversation and initializes the attribute bar (Bar is some entity class). 在对话开始时将调用方法start()并初始化属性bar(Bar是某些实体类)。 Then, bar has an attribute baz which is also an entity.
然后,bar具有属性baz,它也是一个实体。
During a conversation, it might happen that some other user modifies the baz object related to our bar. 在对话期间,其他用户可能会修改与我们的酒吧相关的baz对象。 Then, when end() is called, bar.getBaz() returns the old (unmodified) version of baz, and, moreover, overwrites it to the database.
然后,在调用end()时,bar.getBaz()返回baz的旧版本(未修改),并且将其覆盖到数据库中。
I tried to get the modified state of baz in the end() method using session.flush(), session.merge(bar), session.merge(bar.getBaz()), but nothing works. 我试图使用session.flush(),session.merge(bar),session.merge(bar.getBaz())在end()方法中获取baz的修改状态,但是没有任何效果。 How can I avoid pollution in the database?
如何避免数据库污染?
Sorry that I can't comeup with a elaborate answer. 抱歉,我无法给出详尽的答案。 I had to reload entities from the DB which where modified by another serverlet.
我不得不从数据库中重新加载由另一个serverlet修改过的实体。 I did this by using the
refresh()
method from JPA. 我通过使用JPA的
refresh()
方法做到了这一点。 A Hibernate session also provides the refresh()
method. Hibernate会话还提供了
refresh()
方法。 You might also want to consider the @Version
annotation for optimistic locking. 您可能还需要考虑使用
@Version
批注进行乐观锁定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.