# Query specified join fetching, but the owner of the fetched association was not present in the select list

**URL:** <https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258>\
**Category:** Hibernate ORM\
**Created:** [February 21, 2018, 6:17am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258 "2018-02-21T06:17:16Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiva](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/shiva/32/760_2.png) [@shiva](https://discourse.hibernate.org/u/shiva)\
**Post date:** [February 21, 2018, 6:17am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/1 "2018-02-21T06:17:16Z")

</div>

```auto
I am trying to execute a DTO projection and observe below error while loading this query. Please help on this issue.

_ **org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list** _

SELECT new EmployeeDTO(e.obj1,e.obj2,e.obj3,e.startDate,e.endDate) 
FROM Employee e 
left outer join fetch e.obj1  
left outer join fetch e.obj2 
left outer join fetch e.obj3
where
e.startDate <= :date

```

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [February 21, 2018, 6:39am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/2 "2018-02-21T06:39:06Z")

</div>

Just remove the `fetch` keyword and it will work.

The fetch directive tells Hibernate to include the association in the SELECT clause, but you don’t want that.

What you want is a [DTO projection](https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/) instead which does not require the FETCH directive.

---

<div class="post-metadata">

**Author:** ![shiva](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/shiva/32/760_2.png) [@shiva](https://discourse.hibernate.org/u/shiva)\
**Post date:** [February 21, 2018, 10:03am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/3 "2018-02-21T10:03:54Z")

</div>

Thanks Vlad. It works now. But, I see a different error as below

ERROR: Cannot create TypedQuery for query with more than one return

I followed this article - [https://vladmihalcea.com/hibernate-query-cache-dto-projection/](https://vladmihalcea.com/hibernate-query-cache-dto-projection/)

If I use this way, it works.

List\<EmployeeDTO[]\> employeedto = em.createNamedQuery(namedquery, EmployeeDTO[].class)

But, I would like to use as

List employeedto = em.createNamedQuery(namedquery, EmployeeDTO.class)

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [February 21, 2018, 10:09am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/4 "2018-02-21T10:09:20Z")

</div>

You forgot to add the code for `namedquery`.

Most likely, you selected something else besides the DTO constructor result.

---

<div class="post-metadata">

**Author:** ![shiva](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/shiva/32/760_2.png) [@shiva](https://discourse.hibernate.org/u/shiva)\
**Post date:** [February 21, 2018, 10:21am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/5 "2018-02-21T10:21:44Z")

</div>

```auto
Sorry, named query is here.

select new EmployeeDTO(e.obj1,e.obj2,e.obj3,e.startDate,e.endDate) 
FROM Employee e 
left outer join fetch e.obj1  
left outer join fetch e.obj2 
left outer join fetch e.obj3
where
e.startDate <= :date

DTO Constructor:

public EmployeeDTO(Object obj1, Object obj2,Object obj3,Date startDate, Date endDate) {
		this.obj1= obj1;
		this.obj2= obj2;
		this.obj3= obj3;
		this.startDate = startDate;
		this.endDate = endDate;		
	}
	

Here is the entity

public class Employee implements Serializable {

	@Temporal(TemporalType.DATE)
	private Date endDate;
	
		@Temporal(TemporalType.DATE)
	private Date startDate;

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "XYZ")
	private Object1 obj1;

	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "ABX")
	private Object2 obj2;

	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "ABC")
	private Object3 obj3

}

```

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [February 21, 2018, 10:32am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/6 "2018-02-21T10:32:33Z")

</div>

You can use `@NamedQuery` with DTO projections via JPA constructor result as demonstrated by this [test case](https://github.com/vladmihalcea/high-performance-java-persistence/commit/015e6f4f3545a765e5af3aa3c260ac4e2aa3ac3a#diff-4b86e542c59ec0e8b916161cbd640cdcR17).

However, you don’t have a DTO projection here. You try to fetch entities: obj1, obj2, …

If you do that, Hibernate will probably pass the entity identifier, not the entire entity.

More, the `EmployeeDTO` is identical with the `Employee` entity, so what is the point of using a DTO?

Just use the `Employee` entity if you need all associations:

```auto
SELECT e
FROM Employee e 
left outer join fetch e.obj1  
left outer join fetch e.obj2 
left outer join fetch e.obj3
where
e.startDate <= :date

```

---

<div class="post-metadata">

**Author:** ![shiva](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/shiva/32/760_2.png) [@shiva](https://discourse.hibernate.org/u/shiva)\
**Post date:** [February 21, 2018, 10:44am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/7 "2018-02-21T10:44:51Z")

</div>

Vlad, I could do the same to fetch Employee entities as suggested, however, when I do this I see the memory issue as mentioned in this post. Each query fetch will retrieve nearly 20-30K results. As also, I have a requirement where the fetched Employee List will be iterated so that all the child entity objects will in turned be mapped to their respective DTO’s. So I thought I can refine the code so that I can still manage with one DTO.

> [@QueryImpl consuming more memory](https://discourse.hibernate.org/t/queryimpl-consuming-more-memory/251/2):
>
> What you see there is probably the objects that haven’t been collected by the JVM GC, but the proble might be that you are fetchcing more data than necessary. Try using DTO projections because they are more efficient both on the database and on the client side. More, if you have huge Result Sets but only require 10 entries, better use pagination.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [February 21, 2018, 11:55am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/8 "2018-02-21T11:55:29Z")

</div>

Try with pagination first.

Then, for the DTO, you have to select just the columns needed by the UI.

---

<div class="post-metadata">

**Author:** ![Kapil\_Sharma](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@Kapil\_Sharma](https://discourse.hibernate.org/u/Kapil_Sharma)\
**Post date:** [December 19, 2018, 8:16am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/11 "2018-12-19T08:16:29Z")

</div>

But if we don’t use fetch then N+1 query problem occurs. Is there any way that we can use fetch and as well as this problem can be solved.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [December 19, 2018, 8:41am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/12 "2018-12-19T08:41:23Z")

</div>

You are allowed to use `fetch` to avoid N+1 query issues. This question was about using JOIN FETCH with a projection, which is not a good combination. Check out [this article](https://vladmihalcea.com/how-to-detect-the-n-plus-one-query-problem-during-testing/) for more details about avoiding N+1 query issues.

---

<div class="post-metadata">

**Author:** ![Kapil\_Sharma](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@Kapil\_Sharma](https://discourse.hibernate.org/u/Kapil_Sharma)\
**Post date:** [December 19, 2018, 9:38am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/13 "2018-12-19T09:38:56Z")

</div>

I used fetch to avoid N+1 query but when i am using fetch and applying limit using pageable then i am getting this execption org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list . I am using specification and fetching using left outer join type.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [December 19, 2018, 10:19am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/14 "2018-12-19T10:19:41Z")

</div>

> I used fetch to avoid N+1 query

That’s only required if you fetch entities, not DTOs.

> I am using specification and fetching using left outer join type.

What you want is a [DTO projection](https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/) instead which does not require the FETCH directive.

---

<div class="post-metadata">

**Author:** ![Kapil\_Sharma](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@Kapil\_Sharma](https://discourse.hibernate.org/u/Kapil_Sharma)\
**Post date:** [December 20, 2018, 1:58pm UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/15 "2018-12-20T13:58:04Z")

</div>

That’s only required if you fetch entities, not DTOs.

Yes I am fetching entities.

DTO Projection i cannot use as my column name for where condition changes based on condition. so, for this purpose, i used specification. In DTO projection, a column name cannot be a variable.

Can you please suggest something that i can use fetch and specification and this exception could be resolved.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [December 20, 2018, 2:20pm UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/16 "2018-12-20T14:20:27Z")

</div>

If you read the entire thread carefully, you are going to find the solution.

---

<div class="post-metadata">

**Author:** ![laxman20](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/laxman20/32/407_2.png) [@laxman20](https://discourse.hibernate.org/u/laxman20)\
**Post date:** [December 20, 2018, 7:02pm UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/17 "2018-12-20T19:02:21Z")

</div>

Hi, I read the threads, but I don’t see how they solve the issue.

I have a query that looks something like this:

> SELECT NEW DTO(attribute1, attribute2, entity)  
> FROM Entity entity  
> LEFT JOIN FETCH entity.collection

That’s a very basic illustration of what I’m trying to do. Hibernate doesn’t allow this. If I remove the “fetch”, then I will run into the N+1 select problem when accessing dto.entity.collection.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [December 20, 2018, 7:35pm UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/18 "2018-12-20T19:35:08Z")

</div>

> I have a query that looks something like this:
> 
> ```auto
> SELECT NEW DTO(attribute1, attribute2, entity)
> FROM Entity entity
> LEFT JOIN FETCH entity.collection
> 
> ```

That’s not a valid JPQL query.

You either select entities:

```auto
SELECT entity
FROM Entity entity
LEFT JOIN FETCH entity.collection

```

or a projection which wraps the `Object[]` into a DTO:

```auto
SELECT NEW DTO(attribute1, attribute2, entity.name, col.title)
FROM Entity entity
LEFT JOIN entity.collection col

```

You cannot combine fetching entities and DTOs.

---

<div class="post-metadata">

**Author:** ![Kapil\_Sharma](https://avatars.discourse-cdn.com/v4/letter/k/ea5d25/32.png) [@Kapil\_Sharma](https://discourse.hibernate.org/u/Kapil_Sharma)\
**Post date:** [December 21, 2018, 7:12am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/19 "2018-12-21T07:12:15Z")

</div>

My point is my column name is changing too.

SELECT NEW DTO(attribute1, attribute2, entity.name, col.title)  
FROM Entity entity  
LEFT JOIN entity.collection col where :columnName = :columnValue

but @param for columnName does not work. That’s why I used specification instead of projection. And in specification i tried root.join also which cause N+1 Query issue while root.fetch is causing this exception org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [December 21, 2018, 7:36am UTC](https://discourse.hibernate.org/t/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-not-present-in-the-select-list/258/20 "2018-12-21T07:36:47Z")

</div>

Then you need to use Criteria API if the filtering criteria is variable.
