# How to force join type for count query?

**URL:** https://discourse.hibernate.org/t/how-to-force-join-type-for-count-query/8920
**Category:** Hibernate ORM
**Created:** [January 29, 2024, 10:30am UTC](https://discourse.hibernate.org/t/how-to-force-join-type-for-count-query/8920 "2024-01-29T10:30:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Juan\_Carlos\_Gonzalez](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/juan_carlos_gonzalez/32/2969_2.png) [@Juan\_Carlos\_Gonzalez](https://discourse.hibernate.org/u/Juan_Carlos_Gonzalez)
#### Post date: [January 29, 2024, 10:30am UTC](https://discourse.hibernate.org/t/how-to-force-join-type-for-count-query/8920/1 "2024-01-29T10:30:31Z")

</div>

Hi.

I’m trying to implement a Vaadin application where some data is displayed in a grid. The user can filter the data displayed in the grid and paginate over it.

An example of this is the entity Sales which defines a ManyToOne(optional=true) relation to the entity Customers, as not all the sales require a customer assigned. I’m using a NamedEntityGraph from Sales to Customers in order to fetch customer name in the findAll(Specification) method to populate the grid. The fetch sql is executing a left join between sales table and customers table which is fine. However, the count query (required by vaadin pagination) is executing an inner join, which is not fine as it’s ignoring sales whithout customer assigned.

I don’t know how to force count method to use the EntityGraqh or even if this is possible. If no, please could you explain to me an alternative way for accomplishing this?.

The stack is using Spring Boot 3.0.5, Hibernate 6.1.7.Final and Vaadin 24.

Best regards,

---

<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: [January 29, 2024, 11:12am UTC](https://discourse.hibernate.org/t/how-to-force-join-type-for-count-query/8920/2 "2024-01-29T11:12:50Z")

</div>

Hi, this is a forum for Hibernate ORM questions. You seem to have a problem with some sort of Vaadin extension or Spring Data that try to generate a query for you.  
Unless you have a problem with Hibernate ORM itself, please post your questions to the forums of the respective extension vendors, as nobody in this forum has an idea about what these tools are doing.

---

<div class="post-metadata">

### Author: ![Juan\_Carlos\_Gonzalez](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/juan_carlos_gonzalez/32/2969_2.png) [@Juan\_Carlos\_Gonzalez](https://discourse.hibernate.org/u/Juan_Carlos_Gonzalez)
#### Post date: [January 29, 2024, 11:40am UTC](https://discourse.hibernate.org/t/how-to-force-join-type-for-count-query/8920/3 "2024-01-29T11:40:17Z")

</div>

Hi.

Thanks for your quick response and sorry if it’s not the proper forum for it. I’ve included information regarding the stack in order to provide some context for my question.

Please, let me reformulate my question. Basically, my question is if I’m able to execute a query generated with CriteriaBuilder, which is using a NamedEntityGraph, how can I execute a count query with the same criteria and entity graph whicch was applied to the fecth query?.

Best regards,

---

<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: [January 29, 2024, 12:38pm UTC](https://discourse.hibernate.org/t/how-to-force-join-type-for-count-query/8920/4 "2024-01-29T12:38:34Z")

</div>

You don’t need an entity graph for a count query, because you are not fetching anything. Since Hibernate ORM 6.4 you can create a count query from an existing `CriteriaQuery`: [Hibernate ORM User Guide](https://docs.jboss.org/hibernate/orm/6.4/userguide/html_single/Hibernate_User_Guide.html#criteria-extensions-count-query)

---

<div class="post-metadata">

### Author: ![Juan\_Carlos\_Gonzalez](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/juan_carlos_gonzalez/32/2969_2.png) [@Juan\_Carlos\_Gonzalez](https://discourse.hibernate.org/u/Juan_Carlos_Gonzalez)
#### Post date: [January 29, 2024, 1:23pm UTC](https://discourse.hibernate.org/t/how-to-force-join-type-for-count-query/8920/5 "2024-01-29T13:23:55Z")

</div>

Thank you very much for your response. I’ll try your suggestion. As we’re in an early phase, I don’t think there is any problem moving to Hibernate ORM 6.4.

Best regards,
