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
Post a Comment