playframework hibernate不可变实体和通过合并更改的版本

时间:2013-06-21 15:29:46

标签: java hibernate playframework persistence immutability

我想确保

  1. 我可以使用/来使用不可变实体和Hibernate(使用play-framework)
  2. 当我将实体保存/退回数据库时,这种乐观策略适用于我的实体(版本正在更改
  3. 有测试:

     @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;
    ...
    

0 个答案:

没有答案