Spring JPA 查询的时候提示错 org.hibernate.TransientObjectException

错误的信息为:

Caused by: java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing

问题和解决

出现这个问题的情况有很多。

比如说在对多的关系中,没有进行映射,或者 Lazy Load 的问题的。

在这个地方的问题,我们的情况是调用 Repository 发送了一个空对象。

MlsOffice mlsOffice = mlsOfficeRepository.findDistinctFirstByMlsOfficeIdEqualsAndConfRetsEquals(officeId, new ConfRets());

我们可以通过后面 new 一个对象,然后再进行查询。

这个查询将会重现上面的错误。

因此,我们需要确定在查询的时候发送到后端的对象不应该为 null。

在代码中进行空对象检查是有必要的。