# Hibernate @ManyToMany with FetchMode.JOIN and FetchType.EAGER generates secondary queries

**URL:** <https://discourse.hibernate.org/t/hibernate-manytomany-with-fetchmode-join-and-fetchtype-eager-generates-secondary-queries/2210>\
**Category:** Hibernate ORM\
**Created:** [February 4, 2019, 9:44pm UTC](https://discourse.hibernate.org/t/hibernate-manytomany-with-fetchmode-join-and-fetchtype-eager-generates-secondary-queries/2210 "2019-02-04T21:44:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeigler](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@zeigler](https://discourse.hibernate.org/u/zeigler)\
**Post date:** [February 4, 2019, 9:44pm UTC](https://discourse.hibernate.org/t/hibernate-manytomany-with-fetchmode-join-and-fetchtype-eager-generates-secondary-queries/2210/1 "2019-02-04T21:44:56Z")

</div>

I have an existing web app using Hibernate 5.1. I am in the process of upgrading to 5.3.7. I noticed the deprecated warnings on my Criteria queries so I converted them to use JPA criteria. I have an entity that has a @ManyToMany mapping. This property was a list so I was getting a subselect on queries. I converted my code so that it was now a Set and annotated with @Fetch(FetchMode.JOIN) but still am getting subselect behavior.

I have it annotated as:

```auto
    @ManyToMany( cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch = FetchType.EAGER, targetEntity = com.mydomain.model.Role.class)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
	@Fetch(FetchMode.JOIN)

```

Is there any reason I cannot get JOINs on my @ManyToMany properties with JPA criteria based queries?

---

<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 5, 2019, 6:41am UTC](https://discourse.hibernate.org/t/hibernate-manytomany-with-fetchmode-join-and-fetchtype-eager-generates-secondary-queries/2210/2 "2019-02-05T06:41:34Z")

</div>

> [@zeigler](#):
>
> I converted my code so that it was now a Set and annotated with @Fetch(FetchMode.JOIN) but still am getting subselect behavior.

Using `@Fetch(FetchMode.JOIN)` is not needed since you are using `FetchType.EAGER` already. However, using [eager loading is a very bad idea](https://vladmihalcea.com/eager-fetching-is-a-code-smell/) from a performance perspective.

More, using `FetchType.EAGER` or `FetchMode.JOIN` does not guarantee that Hibernate will always employ a JOIN to fetch the entity. A JOIN will be used only if you fetch the entity directly via `entityManager.find`. However, for JPQL or Criteria API, unless you use `JOIN FETCH`, a join will not be used and you will see secondary queries and bump into [N+1 query issues](https://vladmihalcea.com/how-to-detect-the-n-plus-one-query-problem-during-testing/).

> Is there any reason I cannot get JOINs on my @ManyToMany properties with JPA criteria based queries?

Yes, there is a much better way than using `FetchType.EAGER`. You need to use `JOIN FETCH` in your JPQL or Criteria API queries. For more details, check out [this article](https://vladmihalcea.com/the-best-way-to-handle-the-lazyinitializationexception/).
