我正在尝试将一个关系"OneToMany“添加到我正在创建的数据库中,以测试ObjectDB是否对我有任何好处。
到目前为止,我尝试了以下几点:
@Entity
public class Parent {
@Id
private int parentID;
private int childFK;
Set<Child> children;
public Parent(int p,int c) {
this.parentID = p;
this.childFK = c;
}
@OneToMany(orphanRemoval=true)
@JoinColumn(name="childFK")
public Set<Child> getChildren(){
return childern;
}
}这个子类是:
@Entity
public class Child {
@Id
private int childID;
private String name;
public Child(int c,String n) {
this.childID = c;
this.name = n;
}
}我的主要方法是:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Parent p = new Parent(01,02);
em.persist(p);
Child c = new Child(02,"bob");
em.persist(c);
em.getTransaction().commit();这会使数据库没有问题,但不存在任何关系。这是was的文档说明如何做到这一点。
我还尝试将子代的实际实例添加到数据库中。这在某种程度上是可行的,因为它知道有一个数据库实例可用,但又不知道如何使用它。
我已经尝试了这个查询:
SELECT p.getChildren() FROM Parent p这只会返回这样的方法getChildren()。
有什么想法吗?
发布于 2013-04-02 21:18:29
你的测试中有两个问题。修复这些错误后,父进程和子进程之间的关系将按预期创建。
第一个问题,在JPA中,您可以使用字段访问或属性访问,但不允许混合使用它们。如果您使用@Id注释字段,则您使用的是字段访问。在这种情况下,其他注释也应该在字段上,而不是属性方法上。
相应地,您的父类应该定义为:
@Entity
public static class Parent {
@Id
private int parentID;
private int childFK;
@OneToMany(orphanRemoval=true)
// @JoinColumn(name="childFK") ignored by ObjectDB
Set<Child> children;
public Parent(int p,int c) {
this.parentID = p;
this.childFK = c;
}
public Set<Child> getChildren(){
return children;
}
}第二个问题是main方法持久化两个对象,一个父对象和一个子对象,但没有连接它们。您必须添加一行代码来连接这两个对象:
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Parent p = new Parent(01,02);
em.persist(p);
Child c = new Child(02,"bob");
em.persist(c);
p.children = Collections.singleton(c); // connect the child to the parent
em.getTransaction().commit();在使用这些修复程序运行测试之后,将使用关系创建一个ObjectDB数据库。
发布于 2013-06-16 03:47:03
我尝试了解p.children = Collections.singleton(c),但它对我不起作用。这是我对这个问题的看法……让我们像上面的例子一样使用父对象和类对象。
@Entity
public class Parent {
@Id
private int parentID;
private int childFK;
Set<Child> children;
public Parent(int p,int c) {
this.parentID = p;
this.childFK = c;
}
. . .
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="childFK")
public Set<Child> getChildren(){
return childern;
}
public void setChildren(Set<Child> children) {
this.children = children;
}
}
@Entity
public class Child {
@Id
private int childID;
private String name;
private int parentID;
public Child(int c,String n, int p) {
this.childID = c;
this.name = n;
this.parentID = p;
}
}在实际场景中,您需要从某个源获取子值,在本例中,我们只从childID类获取一个id。
public int childLastID(EntityManager em){
TypedQuery<Child> query = em.createQuery("SELECT c FROM Child c order by i.childID
DESC", Child.class);
List<Child> chldx = query.setFirstResult(0).setMaxResults(1).getResultList();
if(chldx == null) return 100; // In case your Child is new and empty
// lets start with 100
return (chldx.get(0)).getId();
}现在让我们填充这些父类和子类...
EntityManagerFactory emf = Persistence.createEntityManagerFactory("...");
EntityManager em = emf.createEntityManager();
int ix = childLastID(em);
em.getTransaction().begin();
Parent p = new Parent(01, ++ix); // increment the last ID to make sure it's unique
Set<Child> cset = new HashSet<Child>();
Child c1 = new Child(ix,"bob", 01);
cset.add(c1);
Child c2 = new Child(++ix,"beb", 01);
cset.add(c2);
Child c3 = new Child(++ix, "bib", 01);
cset.add(c3)
p.setChildren(cset);
em.persist(p)
em.getTransaction().commit();现在,c1孩子的收藏..。c3将持久化在父类上。我在c2和c3上递增了++ix,因为它们在子类上需要有一个唯一的id。我在子类上添加了一个parentID,作为父类和子类的引用。
我使用FetchType.EAGER,这样当检索父对象时,这些子引用也会被检索到。
此外,实体类的ID应该是long类型,而不是int类型,因此只需使用long原语类型更改这些int即可。
希望这能有所帮助..。
Nelson Deogracias
https://stackoverflow.com/questions/15749359
复制相似问题