Thursday, 23 August 2012

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

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

Unidirectional

We set the cascade type (i.e., propagation of operations) to ALL:
@Entity
@AutoProperty
public class OneToManyUnidirectionalA implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
 
    @OneToMany(cascade=CascadeType.ALL)
    private Collection<onetomanyunidirectionalb> b;
 
    private String s;

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

}


@Entity
@AutoProperty
public class OneToManyUnidirectionalB implements Serializable {

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

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

}
The following:
OneToManyUnidirectionalA a = new OneToManyUnidirectionalA("AAA");
  
Collection<OneToManyUnidirectionalB> setB
    = new ArrayList<OneToManyUnidirectionalB>();
  
setB.add(new OneToManyUnidirectionalB("BBB"));
setB.add(new OneToManyUnidirectionalB("CCC"));

a.setB(setB);
  
JPA.INSTANCE.save(a);
JPA.INSTANCE.clear();
  
OneToManyUnidirectionalA retrA
    = JPA.INSTANCE.get(OneToManyUnidirectionalA.class, a.getId());
  
System.out.println("Retriving OneToMany Unidirectional A");
System.out.println(retrA);
  
System.out.println("Retriving OneToMany Unidirectional B's");
for (OneToManyUnidirectionalB origB : a.getB()) {
    OneToManyUnidirectionalB retrB
        = JPA.INSTANCE.get(
            OneToManyUnidirectionalB.class, origB.getId());
    System.out.println(retrB);
}
Generates:
Retriving OneToMany Unidirectional A
OneToManyUnidirectionalA{id: {1}, b: {[OneToManyUnidirectionalB{id: {1}, s: {BBB}}, OneToManyUnidirectionalB{id: {2}, s: {CCC}}]}, s: {AAA}}
Retriving OneToMany Unidirectional B's
OneToManyUnidirectionalB{id: {1}, s: {BBB}}
OneToManyUnidirectionalB{id: {2}, s: {CCC}}

Bidirectional

A owns the relationship:
@Entity
@AutoProperty
public class OneToManyBidirectionalA implements Serializable {

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

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

}


@Entity
@AutoProperty
public class OneToManyBidirectionalB implements Serializable {

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

    @Property(policy=PojomaticPolicy.NONE) 
    @ManyToOne
    private OneToManyBidirectionalA a;

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

}
The following:
OneToManyBidirectionalB b2 = new OneToManyBidirectionalB("BiBBB");
OneToManyBidirectionalB c2 = new OneToManyBidirectionalB("BiCCC");
  
Collection<OneToManyBidirectionalB> setB2
    = new ArrayList<OneToManyBidirectionalB>();
  
setB2.add(b2);
setB2.add(c2);

a2.setB(setB2);
b2.setA(a2);
c2.setA(a2);
  
JPA.INSTANCE.save(a2);
JPA.INSTANCE.clear();
  
OneToManyBidirectionalA retrA2
    = JPA.INSTANCE.get(OneToManyBidirectionalA.class, a2.getId());
  
System.out.println("Retriving OneToMany Bidirectional A");
System.out.println(retrA2);

System.out.println("Retriving OneToMany Bidirectional B's");
    for (OneToManyBidirectionalB origB : a2.getB()) {
        OneToManyBidirectionalB retrB
     = JPA.INSTANCE.get(
         OneToManyBidirectionalB.class, origB.getId());
        System.out.println(retrB);
}
Generates:
Retriving OneToMany Bidirectional A
OneToManyBidirectionalA{id: {1}, b: {[OneToManyBidirectionalB{id: {1}, s: {BiBBB}}, OneToManyBidirectionalB{id: {2}, s: {BiCCC}}]}, s: {BiAAA}}
Retriving OneToMany Bidirectional B's
OneToManyBidirectionalB{id: {1}, s: {BiBBB}}
OneToManyBidirectionalB{id: {2}, s: {BiCCC}}
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.