Hello.

Just upgraded spring boot version from 2.7 to 3.1.5 in my project, and noticed a strange behaviour. The following query is behaving as if it has the distinct keyword without having it.

@Query("SELECT T.COLUMN2 FROM MyTable T WHERE T.COLUMN1 = ?1")

Basically I should be getting a List with a few equal elements, but I'm getting a List with just one element. I verified that this is happening after the upgrade, it was not happening before.

Not sure if i'm in the correct place to report this issue. If not, please name the appropriate channel.

Comment From: philwebb

It's very hard to say where the problem lies, but the chances are fairly high that it's not a direct Spring Boot issue. It's most likely to be something that's changed in Spring Data JPA or Hibernate.

I would start by creating a small sample application that replicates the issue and then turn on debug logging and SQL tracing to see if you can determine where the issues lies and who might be able to help you fix it.

I'm going to close this one for now, but if you do create a sample feel free to share it or to cross reference any new issue that you open with the other project.

Comment From: gabrikid

Hi @philwebb

After some investigation, I concluded that as of hibernate 6, distinct is now applied automatically to some queries, as stated on their migration guide:

Starting with Hibernate ORM 6 it is no longer necessary to use distinct in JPQL and HQL to filter out the same parent entity references when join fetching a child collection. The returning duplicates of entities are now always filtered by Hibernate.

Not sure if there is a way to disable this behaviour. Also, I consider this to be a breaking change, even tho I recognize that most programmers prefer this feature.

Spring boot 2.7 to 3.0 migration guide does not mention this, and given that its a breaking change, I recommend you to add it.

Regarding my software, I believe I have a solution. I'm currently trying to implement it.

Thanks.

Comment From: philwebb

Thanks for letting us know @gabrikid.

Spring boot 2.7 to 3.0 migration guide does not mention this, and given that its a breaking change, I recommend you to add it.

The migration guide does include a section about Hibernate. I don't think we should expand it because it's hard for us to know which changes are important enough to include.