確認したい
- Hibernate で不変エンティティを使用する場合があります(play-framework を使用)
- その楽観的な戦略は、エンティティをDBに保存/マージすると、エンティティ(バージョンが変更されます)に対して機能します
テストがあります:
@Test
public void test() {
// create question->answer and save them to db
QuestionUtil.createQuestion("question1", "answer1");
// detaching question from the hibernate session, let user to think and play with entity
Question question1 = Question.find("title", "question1").first();
Question.em().detach(question1);
// user think/edit time ... [1 minute.. 2 minutes.. coffee.. break time.. ]
// user makes some edits in detached question, create new fresh copy (since it's immutable)
Question editingQuestion = new Question.Builder().copy(question1).title("updated question1").build();
editingQuestion.merge();
// get updated Question
Question updatedQuestion = Question.find("title", "question1").first();
assertEquals(updatedQuestion.getTitle(), "updated question1"); // OK
assertTrue(updatedQuestion .getVersion().intValue() == 1); // FAILS here
}
質問。 バージョンが (0 ではなく) 1 に増えないのはなぜですか? 一般的にこれを行うより良い方法はありますか?
Question クラスのヘッドは次のとおりです。
@Entity
public class Question extends GenericModel {
@Version
private Integer version;
...