JPA/Hibernate duplicate records -


i have one-to-many relationship between entities. when doing jpql query:

select parent parent parent join parent.child child ...

i duplicate records when parent has 2 children, 1 when parent have 1 child, none when there no child (none when no child fine). note there no duplicate of parent in sql database.

the entities declared follow:

@entity(...) public class parent {      @id     long parentid;      @onetomany(mappedby = "parentid")     list<child> children; }  @entity(...) public class child {a      long parentid; } 

i omitted lot of code brevity's sake, should give strong idea of trying do. note relationship defined on parent's side because need list of parents along children returned query.

you can rid of duplicates using distinct keyword:

select distinct parent parent parent join parent.child child ... 

edit: distinct keyword used remoe duplicates query results regardless of teh reason existence of these duplicates. reason duplicate db entries. more often, duplicates consequence of join statements, use case legitimate.

however, avoid explicit joins , distinct keyword making relation bidirectional. can use implicit joins through navigation:

select parent parent parent parent.children... 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -