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). Then, bar has an attribute baz which is also an entity.
During a conversation, it might happen that some other user modifies the baz object related to our bar. Then, when end() is called, bar.getBaz() returns the old (unmodified) version of baz, and, moreover, overwrites it to the database.
I tried to get the modified state of baz in the end() method usin开发者_JS百科g session.flush(), session.merge(bar), session.merge(bar.getBaz()), but nothing works. 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. I did this by using the refresh()
method from JPA.
A Hibernate session also provides the refresh()
method. You might also want to consider the @Version
annotation for optimistic locking.
精彩评论