[hibernate-users] One to one bi-directional mappings

John Baker jbaker at javasystemsolutions.com
Wed Mar 17 09:26:09 EDT 2010


Dear List,

Perhaps you can help?  I've done a number of Hibernate mappings before
now, but something has left me puzzled.  If I have objects A and B, where
A has a one to one bi-directional relationship with B, I define them as
follows:

public class A {
   private B b;

   ...
   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="FK_b", nullable=false)
   public B getB()
   { return b; }
}

public class B {
   private A a;

   @OneToOne(mappedBy="b")
   public A getA()
   { return a; }
}

This works perfectly.

However, what if I want A to have multiple instances of B, i.e.:

public class A {
  protected B b1, b2;

   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="FK_b1", nullable=false)
   public B getB1()
   { return b1; }

   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="FK_b2", nullable=false)
   public B getB2()
   { return b2; }
}

The object B still only has one parent (A), so how is this defined?

Any thoughts are most welcome.


John



More information about the hibernate-users mailing list