JPA+HIBERNATE: cartesian product in Many-to-Many relationship -


i try explain problem. have entity manytomany relationship

@entity @table(name="table1") public class table1 implements serializable {  ...  //bi-directional many-to-many association table1 @manytomany @jointable(     name="table2"     , joincolumns={         @joincolumn(name="id_elements1")         }     , inversejoincolumns={         @joincolumn(name="id_elements2")         }     ) private list<table1> elements;  //bi-directional many-to-many association table1 @manytomany(mappedby="elements") private list<table1> elementof;  ...  } 

db table are:

table1 id      ... 55499   ... 55498   ... 55497   ...  table2 id_elements1    id_elements2 55499           55498 55499           55497 

when try execute following jpql query

select       t         table1 t       left join fetch t.elementof        t.id = 55499 

result arraylist two elements (with id 55499) , every element has arraylist of 2 elements (one id 55498 , 1 id 55497). result obtain one element (with id 55499) arraylist of 2 elements (one id 55498 , 1 id 55497). hope clear. can me optimize java object result (i vaguely remember queryhints.batch in eclipselink)?

you have specified join in query, that's gets executed. jpa provider not remove duplicates automatically.

you can add distinct in query remove duplicates:

select       distinct t         table1 t       left join fetch t.elementof        t.id = 55499 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -