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

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

问题描述

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);
    }
}

当使用 Fixtures.deleteAll() 取消注释该行时,我们将在第二种方法中获得异常 - createSoftwareWithOkAuthor() when save()作者.为什么会这样?

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)

推荐答案

问题似乎是 Hibernate 引发了异常(因此当前事务无效),但随后您正尝试在该会话中继续执行更多操作.

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.

在生产代码(假设是控制器)上,您需要重新启动操作(关闭事务,重新启动进程)才能继续.但是考虑到 play 管理事务的方式,正常的行为是在错误发生后你只会向用户返回一条错误消息.

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.

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

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