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

**URL:** <https://www.isharkfly.com/t/spring-jpa-org-hibernate-transientobjectexception/14145>\
**Category:** 程序语言\
**Created:** [October 12, 2022, 4:37pm UTC](https://www.isharkfly.com/t/spring-jpa-org-hibernate-transientobjectexception/14145 "2022-10-12T16:37:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [October 12, 2022, 4:37pm UTC](https://www.isharkfly.com/t/spring-jpa-org-hibernate-transientobjectexception/14145/1 "2022-10-12T16:37:02Z")

</div>

错误的信息为：

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

```

![2022-10-12_12-34-35](https://www.isharkfly.com/images/transparent.png)

## 问题和解决

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

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

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

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

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

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

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

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