# Specify to fetch lazy entity with Specification

**URL:** <https://discourse.hibernate.org/t/specify-to-fetch-lazy-entity-with-specification/8368>\
**Category:** Hibernate ORM\
**Created:** [October 10, 2023, 7:06pm UTC](https://discourse.hibernate.org/t/specify-to-fetch-lazy-entity-with-specification/8368 "2023-10-10T19:06:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![user-x](https://avatars.discourse-cdn.com/v4/letter/u/839c29/32.png) [@user-x](https://discourse.hibernate.org/u/user-x)\
**Post date:** [October 10, 2023, 7:06pm UTC](https://discourse.hibernate.org/t/specify-to-fetch-lazy-entity-with-specification/8368/1 "2023-10-10T19:06:35Z")

</div>

Hi!  
I use Spring’s `Specification` (that I believe is the analogue or wrapper over Criteria) and want to specify that entity must be loaded eagerly.  
How to do it?  
Trying this way:

```auto
             return spec.and((root, query, criteriaBuilder) -> {
                root.fetch("city", JoinType.LEFT);
                return criteriaBuilder.conjunction();
            });

```

but getting error:

> org.hibernate.query.SemanticException: query specified join fetching, but the owner of the fetched association was not present in the select list [SqmSingularJoin(com.dating.model.User(123736953846700).city(123736954064400) : city)]

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 11, 2023, 9:51am UTC](https://discourse.hibernate.org/t/specify-to-fetch-lazy-entity-with-specification/8368/2 "2023-10-11T09:51:32Z")

</div>

Well, that’s what you get when you use these “great” Spring abstractions 😉

I guess that whatever Spring does involves selecting parts of the query root, but not the root itself. Possibly you’re using a projection? I’d recommend you to use an entity graph instead though.

---

<div class="post-metadata">

**Author:** ![anna1725](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anna1725](https://discourse.hibernate.org/u/anna1725)\
**Post date:** [October 12, 2023, 6:50am UTC](https://discourse.hibernate.org/t/specify-to-fetch-lazy-entity-with-specification/8368/3 "2023-10-12T06:50:07Z")

</div>

The error you’re encountering suggests that you’re trying to join fetch a relationship in your Hibernate Criteria query, but the entity owner of the fetched association is not present in the select list. To resolve this issue, you need to include the owner entity in the select list.

You can modify your code as follows:

javaCopy code

```auto
return spec.and((root, query, criteriaBuilder) -> {
    root.fetch("city", JoinType.LEFT);
    query.distinct(true); // Add this line to include the owner entity in the select list
    return criteriaBuilder.conjunction();
[}]<a href="https://sutogames.com/blog/android-game-development-companies">))</a>

```

By adding `query.distinct(true);`, you ensure that the owner entity (in this case, the User entity) is included in the select list, allowing the fetch operation to work correctly. This should resolve the error you’re encountering.

---

<div class="post-metadata">

**Author:** ![anna1725](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anna1725](https://discourse.hibernate.org/u/anna1725)\
**Post date:** [October 12, 2023, 8:18am UTC](https://discourse.hibernate.org/t/specify-to-fetch-lazy-entity-with-specification/8368/4 "2023-10-12T08:18:51Z")

</div>

The error you’re encountering suggests that you’re trying to join fetch a relationship in your Hibernate Criteria query, but the entity owner of the fetched association is not present in the select list. To resolve this issue, you need to include the owner entity in the select list.

You can modify your code as follows:  
return spec.and((root, query, criteriaBuilder) → {  
root.fetch(“city”, JoinType.LEFT);  
query.distinct(true); // Add this line to include the owner entity in the select list  
return criteriaBuilder.conjunction();  
});  
By adding `query.distinct(true);` , you ensure that the owner entity (in this case, the User entity) is included in the select list, allowing the fetch operation to work correctly. This should resolve the error you’re  
[encountering.](https://sutogames.com/blog/android-game-development-companies/)

---

<div class="post-metadata">

**Author:** ![user-x](https://avatars.discourse-cdn.com/v4/letter/u/839c29/32.png) [@user-x](https://discourse.hibernate.org/u/user-x)\
**Post date:** [October 12, 2023, 9:01am UTC](https://discourse.hibernate.org/t/specify-to-fetch-lazy-entity-with-specification/8368/5 "2023-10-12T09:01:37Z")

</div>

> [@anna1725](#):
>
> query.distinct(true);

thanks, it works!  
saw this in hibernate examples, but thought somewhy that it’s not needed in my case )

---

<div class="post-metadata">

**Author:** ![user-x](https://avatars.discourse-cdn.com/v4/letter/u/839c29/32.png) [@user-x](https://discourse.hibernate.org/u/user-x)\
**Post date:** [October 15, 2023, 7:04pm UTC](https://discourse.hibernate.org/t/specify-to-fetch-lazy-entity-with-specification/8368/6 "2023-10-15T19:04:52Z")

</div>

well… sorry my fault, it’s appeared it’s not fixing the issue.  
Previous time when I reported it was false-positive by tests… probably missed case.
