1 Reply Latest reply on Dec 12, 2007 11:33 AM by Claudio R

    Two Entity has @OneToMany to the same Entity

    Claudio R Novice

      How can I do two Entity has @OneToMany to the same Entity?
      For example the Entity A has @OneToMany to Entity C and Entity B has @OneToMany to Entity C too.
      The Hibernate adds in the Entity C the id of Entity A or Entity B, but in the search the Hibernate doesn´t know of which Entity the id referer (Entity A or Entity B).
      How can I resolve this problem?
      Thanks

        • 1. Re: Two Entity has @OneToMany to the same Entity
          Claudio R Novice

          You can resolve this with:

          @Entity
          public class A implements Serializable{
          
           @Id
           @GeneratedValue(strategy = GenerationType.IDENTITY)
           @Column(name="id_a")
           private int id;
          
           @OneToMany(mappedBy="a", fetch = FetchType.EAGER)
           @Cascade(CascadeType.ALL)
           private Set<C> cs = new HashSet<C>();
          
           .
           .
           .
          
          }
          
          @Entity
          public class B implements Serializable{
          
           @Id
           @GeneratedValue(strategy = GenerationType.IDENTITY)
           @Column(name="id_b")
           private int id;
          
           @OneToMany(mappedBy="b", fetch = FetchType.EAGER)
           @Cascade(CascadeType.ALL)
           private Set<C> cs = new HashSet<C>();
          
           .
           .
           .
          
          }
          
          @Entity
          public class C implements Serializable{
          
           @ManyToOne(fetch = FetchType.EAGER)
           @JoinColumn(name="id_a", insertable=true, updatable=true)
           @Fetch(FetchMode.JOIN)
           @Cascade(CascadeType.SAVE_UPDATE)
           private A a;
          
           @ManyToOne(fetch = FetchType.EAGER)
           @JoinColumn(name="id_b", insertable=true, updatable=true)
           @Fetch(FetchMode.JOIN)
           @Cascade(CascadeType.SAVE_UPDATE)
           private B b;
          
           .
           .
           .
          }