java - Hibernate Mapping - How to Join Three Tables -


i have 3 entity: person, car, , proffesion.

    @entity     @table(name = "person")     public class person implements serializable {          @id         private long id;          @onetomany(mappedby = "person", fetch = fetchtype.eager)         private list<car> cars;          @onetomany(mappedby = "person", fetch = fetchtype.eager)         private list<profession> professions;     } 
    @entity     @table(name = "profession")     public class profession implements serializable {          @id         private long id;          @manytoone         @joincolumn(name = "person_id")         private person person;     } 
    @entity     @table(name = "car")     public class car implements serializable {          @id         private long id;          @manytoone         @joincolumn(name = "person_id")         private person person;     } 

when i'm trying retrieve person connections 2 professions , 1 car result 2 professions , 2 duplicated cars.

or if connected 5 cars , 1 profession, result 5 cars , 5 duplicated professions.

how should correct mappings receive right results?

use set map onetomany if don't want duplicates. mapped <set> element in mapping table. first make changes these parts:

private set<car> car = new hashset<car>(0);  @onetomany(fetch=fetchtype.lazy, mappedby="persons")     public set<car> getcar() {         return this.car;  } public void setcar(set<car> car) {         this.car = car;     } 

do same profession , onetomany not want duplicates. can set fetchtype based on loading preferences. eager load @ once , lazy load on demand best.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -