gpt4 book ai didi

hibernate - save 方法 - 发生异常后不刷新 session

转载 作者:行者123 更新时间:2023-12-03 06:21:30 24 4
gpt4 key购买 nike

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)

最佳答案

来自错误:

org.hibernate.AssertionFailure: null id in models.Software entry (don't flush the Session after an exception occurs)

我们可以看到之前发生过 session 异常。抛出此 org.hibernate.AssertionFailure 的点不是发生错误的点。

即:某些东西正在抑制原始异常。

因此寻找其他可能的错误点。 save()saveOrUpdate() 可能尝试持久保存具有 null 字段的实体,其中表中的列为 非空

就我而言,真实异常发生在 try/catch {} block 内,其中 catch 抑制了异常(没有不要重新抛出或警告我)。

关于hibernate - save 方法 - 发生异常后不刷新 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5298825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com