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()时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)发布于 2011-03-14 13:35:34
问题似乎是Hibernate引发了异常(因此当前事务无效),但随后您将尝试在该会话中继续进行更多操作。
正确的方法是将您在2中使用的测试拆分,一部分用于测试空作者,另一部分用于使用有效的作者进行测试。
在生产代码(例如控制器)上,您需要重新启动操作(关闭事务,重新启动流程)才能继续。但是,考虑到play管理事务的方式,通常的行为是在错误发生后,您只需向用户返回一条错误消息。
发布于 2013-05-15 13:17:51
从错误:
org.hibernate.AssertionFailure:models.Software条目中的null id(异常发生后不要刷新Session)
我们可以看到,--一个会话异常--发生在之前。抛出此org.hibernate.AssertionFailure的点不是引起错误的点。
也就是说:某些东西正在抑制原始异常.
因此,寻找其他可能的错误点。save()或saveOrUpdate()可能试图持久化一个具有null字段的实体,其中表中的列是NOT NULL。
在我的例子中,真正的异常发生在try/catch {}块中,其中catch抑制了异常(没有重新抛出或警告我)。
发布于 2017-03-10 12:29:07
也许有人会重复我的错误:
我也遇到了这个问题。在我的例子中,出现了问题,因为我设置了column类型integer,并试图编写long值。更改column类型后,它开始工作。
https://stackoverflow.com/questions/5298825
复制相似问题