2

私はクラスを持っています:

@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();
    }        
}

メソッド start() は会話の開始時に呼び出され、属性 bar を初期化します (Bar はエンティティ クラスです)。次に、bar にはエンティティでもある属性 baz があります。

会話中に、他のユーザーがバーに関連する baz オブジェクトを変更することがあります。その後、end() が呼び出されると、bar.getBaz() は古い(変更されていない) バージョンの baz を返し、さらにそれをデータベースに上書きします。

session.flush()、session.merge(bar)、session.merge(bar.getBaz()) を使用して end() メソッドで baz の変更された状態を取得しようとしましたが、何も機能しません。データベースの汚染を回避するにはどうすればよいですか?

4

1 に答える 1

1

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.

于 2010-11-28T11:31:57.177 に答える