OpenJPA: @OneToMany mapping with composite primary key

310 views Asked by At
@Entity
@Table(name = "PARENT")
public class ParentClass implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "PARENT_ID")
    private long parentID;

    // uni-directional many-to-one association 
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    private Set<Child> children;
}

@Entity
@Table(name = "CHILD")
public class Child implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ChildPK id;


   // bi-directional many-to-one association
    @ManyToOne
    @JoinColumn(name = "PARENT_ID")
    private Parent parent;
}

@Embeddable
public class ChildPK implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "PARENT_ID")
    private int parentID;

    @Column(name = "CHILD_NM")
    private String childName;
}

 for (Child child : parentClassEntity.getParent().getChildren()) {
    child.getId().setParentID(parentID);
 }
 parentClassEntity.setParentID(parentID);
 dummyDAO.persist(parentClassEntity);

We are using OpenJPA 1.2 (aligned with JSR-220 Java Persistence 1.0 specification). We are also evaluating the solution with OpenJPA 2.3 (aligned with JSR-317 Java Persistence 2.0).

For persisting the child we need to call the merge and delete operations on the parent as well as child, which is leading to explicit two calls to the database.

entityManager.merge(parentEntity);
entityManager.merger (childEntity);

Since this is not automatically being taken care by openJPA and we are trying to evaluate whether OpenJPA can handle this scenario or do we need to explicitly set the values using setter methods (i.e looping thorugh all the instances of the child entities).

Any pointers or directions if can be recommended then it would be much appreciated.

Thanks.

0

There are 0 answers