OpenJPA:@OneToMany映射与复合主键

时间:2015-06-16 19:27:05

标签: java jpa openjpa jpa-1.0

@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);

由于openJPA并没有自动解决这个问题,我们正在尝试评估OpenJPA是否可以处理这种情况,或者我们是否需要使用setter方法显式设置值(即循环遍历子实体的所有实例)。

如果可以推荐任何指针或方向,那么我们将非常感激。

感谢。

0 个答案:

没有答案