# Why is my subquery returning only the ID and not the full entity?

**URL:** <https://discourse.hibernate.org/t/why-is-my-subquery-returning-only-the-id-and-not-the-full-entity/10460>\
**Category:** Hibernate ORM\
**Created:** [October 16, 2024, 3:15pm UTC](https://discourse.hibernate.org/t/why-is-my-subquery-returning-only-the-id-and-not-the-full-entity/10460 "2024-10-16T15:15:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nyx](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@Nyx](https://discourse.hibernate.org/u/Nyx)\
**Post date:** [October 16, 2024, 3:15pm UTC](https://discourse.hibernate.org/t/why-is-my-subquery-returning-only-the-id-and-not-the-full-entity/10460/1 "2024-10-16T15:15:33Z")

</div>

I’m encountering an issue, and I’m using the following as a test example. The subquery is expected to return a full `Person` entity, but instead, it’s only returning the ID. Here’s a simplified version of the query:

TypedQuery\<Object\> query = entityManager.createQuery("SELECT pe, (SELECT pe1 FROM Person pe1 WHERE pe1.nm = pe.nm AND pe1.active = true) " +  
“FROM Person pe”, Object.class);

In the result, the subquery (`pe1`) only includes the `ID` instead of the full `Person` object. This used to work as expected in **Hibernate 4.3** , where the subquery returned the complete object. Any ideas on what might be causing this behavior in the current version?

---

<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 17, 2024, 2:59pm UTC](https://discourse.hibernate.org/t/why-is-my-subquery-returning-only-the-id-and-not-the-full-entity/10460/2 "2024-10-17T14:59:12Z")

</div>

This might have worked in previous Hibernate versions by accident, but is generally not supported. It’s easy to rewrite the query to a supported form:

```sql
select pe, pe2
from Person pe
left join Person pe2 on pe.nm=pe2.nm and pe2.active

```

---

<div class="post-metadata">

**Author:** ![Nyx](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@Nyx](https://discourse.hibernate.org/u/Nyx)\
**Post date:** [October 17, 2024, 3:14pm UTC](https://discourse.hibernate.org/t/why-is-my-subquery-returning-only-the-id-and-not-the-full-entity/10460/3 "2024-10-17T15:14:48Z")

</div>

Thanks you very much!!
