我想确保
- 我可以使用/拥有 Hibernate 的不可变实体(使用播放框架)
- 当我将实体保存/保存到数据库时,该乐观策略适用于我的实体(版本正在更改)
有测试:
@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
}
问题。 为什么版本不增加到 1(而不是 0)?一般有没有更好的方法来做到这一点?
问题班的负责人在这里:
@Entity
public class Question extends GenericModel {
@Version
private Integer version;
...