我想添加一個關係「OneToMany」到我創建的數據庫來測試ObjectDB對我是否有好處。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();
這將創建數據庫中沒有任何問題,但沒有關係存在。 這是文件說明如何做到這一點。
我也嘗試將實際的子實例添加到數據庫中。這有點作用,因爲它知道有一個可用數據庫的實例,但它不知道如何使用它。
我曾嘗試查詢:
SELECT p.getChildren() FROM Parent p
這只是返回沒有這種方法的getChildren()。
任何想法?
如果添加了減1請發表評論我要說明原因。沒有人會從這樣做中受益。謝謝 – Sagarmichael
究竟是什麼問題?問題是沒有孩子嗎?你是否嘗試過'SELECT p FROM Parent p',然後在返回的父對象上調用'getChildren()'? – Pace