ses*_*ses 13 hibernate playframework
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);
}
}
Run Code Online (Sandbox Code Playgroud)
当取消注释该行时,Fixtures.deleteAll()
我们将在第二种方法中获得异常 - createSoftwareWithOkAuthor()
当save()
作者.为什么会这样?
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)
Run Code Online (Sandbox Code Playgroud)
acd*_*ior 11
从错误:
org.hibernate.AssertionFailure
:null id
在models.Software
入口中(不要Session
在发生异常后刷新)
我们可以看到之前发生过会话异常.其中,该点org.hibernate.AssertionFailure
被抛出不到哪错误内容时发生了点.
那就是:有些东西正在压制原来的例外.
因此,寻找其他可能的错误点.A save()
或者saveOrUpdate()
可能尝试持久化具有null
字段的实体,其中列在表中NOT NULL
.
在我的情况下,真正的例外发生在一个try/catch {}
阻止异常的区块内catch
(没有重新抛出或警告我).
问题似乎是Hibernate引发了一个异常(因此当前事务失效)但是你试图在该会话中继续进行更多的操作.
执行此操作的正确方法是将您正在使用的测试拆分为2,一部分用于测试空作者,另一部分用有效作者进行测试.
在生产代码(比如说控制器)上,您需要重新启动操作(关闭事务,重新启动进程)才能继续.但是考虑到play管理事务的方式,正常的行为是在错误之后你将返回错误消息给用户.
归档时间: |
|
查看次数: |
36139 次 |
最近记录: |