1

I have two object like this

public class Foo {
    //something
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "tid")
    Set<Bar> barSet;
}

public class Bar {
    //I don't use many-to-one here
}

when I query Foo using HQL look like this:

FROM Foo f JOIN FETCH f.barSet

I have the amount of duplicated records exactly the same as the amount of Bar's records. How can I fix that?

Thanks a lot!

  • Are you seeing duplicates when you receive the list of Foo entity? The full query is exactly `FROM Foo f JOIN FETCH f.barSet`? Btw, the question is not duplicated, I voted to reopen. – Dherik Mar 23 '18 at 11:40
  • @Dherik yeah, the number of entities I get is exacly the same as the number of record when you inner join two table. For instance, Foo has relation with 10 Bars and we have 10 Foo from that query above. I solved the problem by using a Set to store queried Foo entites. But that's kinda weird with such a good framework like Hibernate. – Aperture Prometheus Mar 23 '18 at 16:21
  • I'm sure that is something not usual in the code. Try to declare the `SELECT`: `SELECT f FROM Foo f JOIN FETCH f.barSet`. I already see a lot of [weird problems](https://stackoverflow.com/a/28649270/2387977) from queries not declaring it. – Dherik Mar 23 '18 at 16:26
  • yeah still. That problem is just occured when I map two table with an unidirectional one-to-many. Every thing is fine with bidirectional one-to-many, many-to-many and of course one-to-one. – Aperture Prometheus Mar 23 '18 at 16:39

0 Answers0