Thursday, 23 August 2012

JPA Many-To-One Relationships (Unidirectional and Bidirectional)

This post illustrates simple many-to-one relationships between two classes.

Unidirectional

@Entity
@AutoProperty
public class ManyToOneUnidirectionalA implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
 
    @ManyToOne
    private ManyToOneUnidirectionalB b;
 
    private String s;

    // Setters, Getters, Constructors, Pojomatic...

}


@Entity
@AutoProperty
public class ManyToOneUnidirectionalB implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
 
    private String s;

    // Setters, Getters, Constructors, Pojomatic...

}
The following:
ManyToOneUnidirectionalA a1 = new ManyToOneUnidirectionalA("AA1");
ManyToOneUnidirectionalA a2 = new ManyToOneUnidirectionalA("AA2");
  
ManyToOneUnidirectionalB b = new ManyToOneUnidirectionalB("BBB");
  
a1.setB(b);
a2.setB(b);
  
JPA.INSTANCE.save(b);
JPA.INSTANCE.save(a1);
JPA.INSTANCE.save(a2);
JPA.INSTANCE.clear();
  
ManyToOneUnidirectionalA retrA1
    = JPA.INSTANCE.get(ManyToOneUnidirectionalA.class, a1.getId());
  
System.out.println("Retrieving ManyToOne Unidirectional A's");
System.out.println(retrA1);
  
ManyToOneUnidirectionalA retrA2
    = JPA.INSTANCE.get(ManyToOneUnidirectionalA.class, a2.getId());
  
System.out.println(retrA2);

System.out.println("Retrieving ManyToOne Unidirectional B");
ManyToOneUnidirectionalB retrB
    = JPA.INSTANCE.get(ManyToOneUnidirectionalB.class, b.getId());
  
System.out.println(retrB);
Generates:
Retrieving ManyToOne Unidirectional A's
ManyToOneUnidirectionalA{id: {1}, b: {ManyToOneUnidirectionalB{id: {1}, s: {BBB}}}, s: {AA1}}
ManyToOneUnidirectionalA{id: {2}, b: {ManyToOneUnidirectionalB{id: {1}, s: {BBB}}}, s: {AA2}}
Retrieving ManyToOne Unidirectional B
ManyToOneUnidirectionalB{id: {1}, s: {BBB}}

Bidirectional

A owns the relationship. We need to avoid Pojomatic circular reference issues too:
@Entity
@AutoProperty
public class ManyToOneBidirectionalA implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
 
    @Property(policy=PojomaticPolicy.NONE) 
    @ManyToOne
    private ManyToOneBidirectionalB b;
 
    private String s;

    // Setters, Getters, Constructors, Pojomatic...

}


@Entity
@AutoProperty
public class ManyToOneBidirectionalB implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
 
    @Property(policy=PojomaticPolicy.NONE) 
    @OneToMany(cascade=CascadeType.ALL, mappedBy="b")
    private Collection<ManyToOneBidirectionalA> a;
   
    private String s;

    // Setters, Getters, Constructors, Pojomatic...

}
The following:
ManyToOneBidirectionalA a11 = new ManyToOneBidirectionalA("BiAAA");
ManyToOneBidirectionalA a22 = new ManyToOneBidirectionalA("BiAAA");
ManyToOneBidirectionalB b2 = new ManyToOneBidirectionalB("BiBBB");
  
Collection<ManyToOneBidirectionalA> c
    = new ArrayList<ManyToOneBidirectionalA>();
c.add(a11);
c.add(a22);
b2.setA(c);
  
a11.setB(b2);
a22.setB(b2);
  
JPA.INSTANCE.save(b2);
JPA.INSTANCE.clear();

ManyToOneBidirectionalB retrB
    = JPA.INSTANCE.get(ManyToOneBidirectionalB.class, b2.getId());
  
System.out.println("Retrieving ManyToOne Bidirectional B");
System.out.println(retrB);
  
for (ManyToOneBidirectionalA orig : b2.getA()) {
    ManyToOneBidirectionalA retrA
        = JPA.INSTANCE.get(
     ManyToOneBidirectionalA.class, orig.getId());
    System.out.println(retrA);
}  
  
System.out.println("Retrieving ManyToOne Bidirectional A's");
  
ManyToOneBidirectionalA retrA11
    = JPA.INSTANCE.get(ManyToOneBidirectionalA.class, a11.getId());
  
System.out.println(retrA11);
System.out.println(retrA11.getB());
  
ManyToOneBidirectionalA retrA22
    = JPA.INSTANCE.get(ManyToOneBidirectionalA.class, a22.getId());
  
System.out.println(retrA22);
System.out.println(retrA22.getB());
Generates:
Retrieving ManyToOne Bidirectional B
ManyToOneBidirectionalB{id: {1}, s: {BiBBB}}
ManyToOneBidirectionalA{id: {1}, s: {BiAAA}}
ManyToOneBidirectionalA{id: {2}, s: {BiAAA}}
Retrieving ManyToOne Bidirectional A's
ManyToOneBidirectionalA{id: {1}, s: {BiAAA}}
ManyToOneBidirectionalB{id: {1}, s: {BiBBB}}
ManyToOneBidirectionalA{id: {2}, s: {BiAAA}}
ManyToOneBidirectionalB{id: {1}, s: {BiBBB}}
The above examples are available from Github in the JPA directory. They rely on Pojomatic too. Some errors messages will be displayed because of a known and harmless issue.

No comments:

Post a Comment