@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 方法显式设置值(即循环遍历子实体的所有实例)。
如果可以推荐任何指针或方向,那么将不胜感激。
谢谢。