# Hibernate-orm joins on fetch not working

**URL:** <https://discourse.hibernate.org/t/hibernate-orm-joins-on-fetch-not-working/5820>\
**Category:** Hibernate ORM\
**Created:** [November 5, 2021, 10:39am UTC](https://discourse.hibernate.org/t/hibernate-orm-joins-on-fetch-not-working/5820 "2021-11-05T10:39:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratul\_sharker](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ratul_sharker/32/1696_2.png) [@ratul\_sharker](https://discourse.hibernate.org/u/ratul_sharker)\
**Post date:** [November 5, 2021, 10:39am UTC](https://discourse.hibernate.org/t/hibernate-orm-joins-on-fetch-not-working/5820/1 "2021-11-05T10:39:21Z")

</div>

I am using hibernate-orm with spring-data-jpa. I have three entities `A`, `B`, `C` declared as following

```auto
@Entity
public class A {
    @OneToMany(....)
    List<B> listOfB;
}

```

```auto
@Entity
public class B {
     @ManyToOne(...)
     private A a;
    @OneToMany(...)
    private C listOfC;
}

```

```auto
@Entity
public class C {
    @ManyToOne(...)
    private B b;
}

```

My objective is to get `A` and fetch `listOfB` as well, with some condition on entity `C` without fetching it. The following JPQL is working fine

```auto
SELECT a FROM A a
LEFT JOIN FETCH a.listOfB b
LEFT JOIN b.listOfC c
WHERE c.xyz = :xyz

```

When i tried using JPA Specification my Specification looks like following

```auto
(rootA, query, builder) -> {
    Fetch fetch = rootA.fetch(A_.listOfB, JoinType.LEFT);
    ListJoin listJoin = fetch.join(B_.listOfC)
   return builder.equal(listJoin.get(C_.xyz), xyz);
}

```

So i am re-using the implicit join done by the fetch operation. This join is not working in specification. It’s outputting the following JPQL

```auto
SELECT a FROM A a
LEFT JOIN FETCH a.listOfB b
WHERE c.xyz = :xyz

```

Saying that, there is no `c` alias.

I have looked into hibernate github source code. Found out that, there is a class named `QueryStructure.java` responsible for generating JPQL query from the criteria object.

I found the function [renderFetches](https://github.com/hibernate/hibernate-orm/blob/e76241a3091078713dd4b57de085f5fadce5e0db/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/QueryStructure.java#L471) which render the fetches

```auto
@SuppressWarnings({ "unchecked" })
	private void renderFetches(
			StringBuilder jpaqlQuery,
			RenderingContext renderingContext,
			Collection<? extends Fetch> fetches) {
		if ( fetches == null ) {
			return;
		}

		for ( Fetch fetch : fetches ) {
			( (FromImplementor) fetch ).prepareAlias( renderingContext );
			jpaqlQuery.append( renderJoinType( fetch.getJoinType() ) )
					.append( "fetch " )
					.append( ( (FromImplementor) fetch ).renderTableExpression( renderingContext ) );

			renderFetches( jpaqlQuery, renderingContext, fetch.getFetches() );
		}
	}

```

Similarly there is a function [`renderJoins`](https://github.com/hibernate/hibernate-orm/blob/e76241a3091078713dd4b57de085f5fadce5e0db/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/QueryStructure.java#L438) responsible for all the joins.

These two are recursive functions rendering the criteria object tree.

It’s clear that all the joins inside the fetches are ignored. There is no call to function `renderJoins` inside from `renderFetches` which causes the generated query to be incomplete.

Is there any in depth reason why we are not joining inside from a fetch ? If yes then how could i re-use the existing implicit joins done by fetch ?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ratul\_sharker](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ratul_sharker/32/1696_2.png) [@ratul\_sharker](https://discourse.hibernate.org/u/ratul_sharker)\
**Post date:** [November 8, 2021, 4:56am UTC](https://discourse.hibernate.org/t/hibernate-orm-joins-on-fetch-not-working/5820/2 "2021-11-08T04:56:58Z")

</div>

Hibernate test case template is created in support of this issue.

- [Test case file](https://github.com/ratulSharker/hibernate-test-case-templates/blob/join-on-fetch-not-working/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java)

This issue is also reported into hibernate issue tracker [HHH-14916](https://hibernate.atlassian.net/browse/HHH-14916)

---

<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:** [November 11, 2021, 10:08am UTC](https://discourse.hibernate.org/t/hibernate-orm-joins-on-fetch-not-working/5820/3 "2021-11-11T10:08:41Z")

</div>

Thanks for the report and test. If you could provide a PR against hibernate-orm with the test and a fix, that would be very helpful!

Either way, beware that this might alter the fetched objects which can lead to data deletion. That’s why it is not easily possible to do this kind of stuff.

---

<div class="post-metadata">

**Author:** ![ratul\_sharker](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ratul_sharker/32/1696_2.png) [@ratul\_sharker](https://discourse.hibernate.org/u/ratul_sharker)\
**Post date:** [November 14, 2021, 12:18pm UTC](https://discourse.hibernate.org/t/hibernate-orm-joins-on-fetch-not-working/5820/4 "2021-11-14T12:18:26Z")

</div>

Hi @beikov

I have created a [pull request](https://github.com/hibernate/hibernate-orm/pull/4365). I have also included test cases. Please review my pull request.

I could not move the ticket [HHH-14916](https://hibernate.atlassian.net/browse/HHH-14916) into **In Progress** because of the permission requirement to move the issue.
