开发者

SEAM/Hibernate: I can't get changes from database during a Conversation

开发者 https://www.devze.com 2023-01-27 02:08 出处:网络
I have a class: @Name(\"foo\") @Scope(ScopeType.CONVERSATION) public class Foo { @In Session session; @In private Conversation 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号