1

I decided to use Entity Graph capability of JPA 2.1 in my recently started project but I faced with a problem.

It goes well when you extend your graph over ManyToOne relationships but for OneToMany, results get duplicated because my provider (Hibernate) uses a left outer join.

I saw some solutions they came up with Distinct key word to tackle this problem but I think it is a tricky solution, even yet How we can get next level Entities Distinct.

I mean if I have 3 entities A, B and C I will use Distinct on A not B where B has a OneToMany relationship to C then we get duplicated records of B inside A and so on

hyeganeh
  • 348
  • 2
  • 11

1 Answers1

1

I figured it out

When we add Distinct to our Query, what is generated as SQL doesn't have any effect over the result provided from database because as far as I know Distinct keyword acts on all columns lied in front of it. Then the result (in this case) provided from database by Distinct or without would be the same.

But Distinct says to JPA or its provider to get the most left table's records on the result collection distinct. And this is the point which leads us to what we expect as the result.

Please if I misunderstood, correct me

hyeganeh
  • 348
  • 2
  • 11