2013-04-01 77 views
0

我想添加一個關係「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

如果添加了減1請發表評論我要說明原因。沒有人會從這樣做中受益。謝謝 – Sagarmichael

+0

究竟是什麼問題?問題是沒有孩子嗎?你是否嘗試過'SELECT p FROM Parent p',然後在返回的父對象上調用'getChildren()'? – Pace

回答

2

您的測試有兩個問題。在修復這些錯誤之後,父母和孩子之間的關係按照預期創建。

第一個問題,在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; 
    } 
} 

的第二個問題是主要的方法仍然存在兩個對象,父母和孩子,但沒有將它們連接起來。您需要添加一行代碼,用於連接這兩個對象:

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數據庫與關係創建運行測試後。

0

我試過解決方案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,因爲它們需要在Child類上有一個唯一的ID。我在Child類中添加了一個parentID作爲Parent和Child類的參考。

我使用FetchType.EAGER,以便在檢索Parent時還會檢索這些子引用。

另外一個實體類的ID應該是long類型的int類型,所以只需要用long原始類型改變那些int類型即可。

希望這將有助於...

尼爾森Deogracias