Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java playframework hibernate不可变实体和通过合并更改版本_Java_Hibernate_Playframework_Persistence_Immutability - Fatal编程技术网

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

Java playframework hibernate不可变实体和通过合并更改版本,java,hibernate,playframework,persistence,immutability,Java,Hibernate,Playframework,Persistence,Immutability,我想确保 我可以在Hibernate中使用/拥有不可变实体(使用play framework) 这种乐观的策略适用于我的实体(版本正在更改),当我将它们保存/标记回数据库时 有一个测试: @Test public void test() { // create question->answer and save them to db QuestionUtil.createQuestion("question1", "answer1");

我想确保

  • 我可以在Hibernate中使用/拥有不可变实体(使用play framework)
  • 这种乐观的策略适用于我的实体(版本正在更改),当我将它们保存/标记回数据库时
  • 有一个测试:

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

    在找到更新问题之前,是否尝试刷新实体管理器?我猜Hibernate还没有向数据库写入任何内容,因此还没有更新版本。我尝试使用这种技术:按事务包装调用。但是,这对db来说并不安全,不同的测试失败(即“OK测试”失败)Question.em().flush();这也于事无补