保存方法 - 发生异常后不刷新会话 [英] save method - doesn't flush the Session after an exception occurs

查看:110
本文介绍了保存方法 - 发生异常后不刷新会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class SoftwareTest extends UnitTest {

@Before
public void setup(){
Fixtures.deleteAll();如果发表评论,将会失败。为什么?????
}

@Test
public void createSoftwareWithNullAuthor(){

//作者为空时

作者nullAuthor =空值;

软件软件=新软件(software1,description1,nullAuthor);
尝试{
software.save();
失败(作者不应该为空);
}赶上(前的PersistenceException){
}

}


@Test
公共无效createSoftwareWithOkAuthor(){
//当作者确定时
作者okAuthor = new Author(author1,email1)。save(); //错误在这里!

软件software2 =新软件(software2,description2,okAuthor);
Software savedSoftware = software2.save();
assertNotNull(savedSoftware);
assertEquals(savedSoftware,software2);

assertNotNull(savedSoftware.author);
assertEquals(okAuthor,savedSoftware.author);


当用取消注释行时, Fixtures.deleteAll()我们将获得恩在第二种方法例外 - createSoftwareWithOkAuthor()保存()作者。 为什么会发生这种情况?

  org.hibernate.AssertionFailure:null id in models.Software entry(don T冲洗会话中发生异常之后)在org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82 

。在org.hibernate.event.def.DefaultFlushEntityEventListener.getValues( DefaultFlushEntityEventListener.java:190)
在org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
在org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java: 240)
。在org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
。在org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)


解决方案

这个问题似乎是Hibernate引发了一个异常(所以当前事务失效),但是你试图在该会话中继续执行更多操作。 b
$ b

正确的做法是将您正在使用的测试分为两部分,一部分用于测试空作者,一部分用有效作者测试。



在生产代码上(比如说一个控制器),您需要重新启动操作(关闭事务,重新启动进程)才能继续。但考虑到播放管理事务的方式,正常的行为将是在错误发生之后,您只会返回错误消息给用户。

public class SoftwareTest extends UnitTest {

    @Before
    public void setup() {
        Fixtures.deleteAll(); // will fail if comment that. why?????
    }

    @Test
    public void createSoftwareWithNullAuthor()  {

       // when author is null

       Author nullAuthor = null;

       Software software = new Software("software1", "description1", nullAuthor);
       try {
         software.save();
         fail("author should not be null");
       } catch (PersistenceException ex) {
       }

    }


    @Test
    public void createSoftwareWithOkAuthor()  {
       // when author is ok
       Author okAuthor = new Author("author1", "email1").save(); // ERROR HERE!

       Software software2 = new Software("software2", "description2", okAuthor);
       Software savedSoftware = software2.save();
       assertNotNull(savedSoftware);
       assertEquals(savedSoftware, software2); 

       assertNotNull(savedSoftware.author);
       assertEquals(okAuthor, savedSoftware.author);
    }
}

when uncomment the line with Fixtures.deleteAll() we will get en exception in second method - createSoftwareWithOkAuthor() when save() the author. Why that's happened?

org.hibernate.AssertionFailure: null id in models.Software entry (don't flush the Session after an exception occurs)
  at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
  at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
  at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
  at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:240)
  at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
  at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
  at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)

解决方案

the issue seems to be that Hibernate raises an exception (so the current transaction gets invalidated) but then you are trying to proceed with more operations in that session.

The proper way to do this would be to split the test you are using in 2, one part to test null authors and one to test with a valid author.

On production code (let's say a controller), you would need to restart the operation (close the transaction, relaunch the process) to be able to proceed. But given the way play manages transactions, the normal behavior would be that after the error you would just return with an error message to the user.

这篇关于保存方法 - 发生异常后不刷新会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆