2016-07-19 169 views
0

我想列出具有內部集合的對象。我可以保存和檢索對象就好了,但是當我做:用集合獲取對象

parentRepo.findAll()

只有最後一個對象有它的上市子對象,其他人有一個空的集合。

父模型

@ForeignCollectionField(eager = false) private Collection<Child> childs;

兒童模式

@DatabaseField(foreign=true,foreignAutoRefresh=true) private Parent parent;

渴望真正沒有任何區別。如果我查詢一個孩子並得到它的父母,我也可以得到它的孩子。我錯過了什麼?

編輯:

它正在爲我所做的建模工作。我的錯誤是我需要父母和孩子之間的多對多關係。我做了一個快速的研究,我需要的是實現這一目標的中間模型。我將結束這個問題,並試圖在我的模型之間建立這種多對多的關係。

+0

我只是意識到我需要一個多對多的關係,而不是多對一的關係。 – user1411166

回答

2

我解決了像這樣的多對多關係: 這是一個正在進行的項目的例子。準備和GlideWax之間有多對多的關係。爲了解決它,我使用你的課:準備,GlideWax和PreparationGlideWax。 PreparationGlideWax表示其他類之間的連接,就像您通常與關係中的表之間的「鏈接」表所解決的多對多關係一樣。正如你所看到的GripWax和Structure也有許多關係準備。下面是代碼:

GlideWax.java

@DatabaseTable(tableName = "glide_waxes") 
public class GlideWax { 

    @DatabaseField(id = true) 
    private int id; 

    @DatabaseField(canBeNull = false) 
    private String name; 

    @DatabaseField 
    private String description; 

    @DatabaseField(canBeNull = false) 
    private int inUse; 

    @DatabaseField(foreign=true) 
    private WaxBrand waxBrand; 

    @DatabaseField(foreign=true) 
    private GlideWaxType glideWaxType; 

    @ForeignCollectionField 
    private ForeignCollection<PreparationGlideWax> preparationGlideWaxes; 

    @ForeignCollectionField 
    private ForeignCollection<TestSessionGlideWax> testSessionGlideWaxes; 

    public GlideWax() { 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public int getInUse() { 
     return inUse; 
    } 

    public void setInUse(int inUse) { 
     this.inUse = inUse; 
    } 

    public WaxBrand getWaxBrand() { 
     return waxBrand; 
    } 

    public void setWaxBrand(WaxBrand waxBrand) { 
     this.waxBrand = waxBrand; 
    } 

    public GlideWaxType getGlideWaxType() { 
     return glideWaxType; 
    } 

    public void setGlideWaxType(GlideWaxType glideWaxType) { 
     this.glideWaxType = glideWaxType; 
    } 

    public ForeignCollection<PreparationGlideWax> getPreparationGlideWaxes() { 
     return preparationGlideWaxes; 
    } 

    public void setPreparationGlideWaxes(ForeignCollection<PreparationGlideWax> preparationGlideWaxes) { 
     this.preparationGlideWaxes = preparationGlideWaxes; 
    } 

    public ForeignCollection<TestSessionGlideWax> getTestSessionGlideWaxes() { 
     return testSessionGlideWaxes; 
    } 

    public void setTestSessionGlideWaxes(ForeignCollection<TestSessionGlideWax> testSessionGlideWaxes) { 
     this.testSessionGlideWaxes = testSessionGlideWaxes; 
    } 

}

Preparation.java

@DatabaseTable(tableName = "preparations") 
public class Preparation { 

    @DatabaseField(generatedId=true) 
    private int id; 

    @ForeignCollectionField 
    private ForeignCollection<PreparationGlideWax> preparationGlideWaxes; 

    @ForeignCollectionField 
    private ForeignCollection<PreparationGripWax> preparationGripWaxes; 

    @ForeignCollectionField 
    private ForeignCollection<PreparationStructure> preparationStructures; 

    @DatabaseField(foreign=true, canBeNull = false) 
    private SkiPair skiPair; 

    @DatabaseField(foreign=true, canBeNull = false) 
    private SkiTester skiTester; 

    @DatabaseField(foreign=true) 
    private Rfid rfid; 

    @DatabaseField(foreign=true, canBeNull = false) 
    private TestSession testSession; 

    @ForeignCollectionField 
    private ForeignCollection<Measurement> measurements; 

    public Preparation() { 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public ForeignCollection<PreparationGlideWax> getPreparationGlideWaxes() { 
     return preparationGlideWaxes; 
    } 

    public void setPreparationGlideWaxes(ForeignCollection<PreparationGlideWax> preparationGlideWaxes) { 
     this.preparationGlideWaxes = preparationGlideWaxes; 
    } 

    public ForeignCollection<PreparationGripWax> getPreparationGripWaxes() { 
     return preparationGripWaxes; 
    } 

    public void setPreparationGripWaxes(ForeignCollection<PreparationGripWax> preparationGripWaxes) { 
     this.preparationGripWaxes = preparationGripWaxes; 
    } 

    public ForeignCollection<PreparationStructure> getPreparationStructures() { 
     return preparationStructures; 
    } 

    public void setPreparationStructures(ForeignCollection<PreparationStructure> preparationStructures) { 
     this.preparationStructures = preparationStructures; 
    } 

    public SkiPair getSkiPair() { 
     return skiPair; 
    } 

    public void setSkiPair(SkiPair skiPair) { 
     this.skiPair = skiPair; 
    } 

    public SkiTester getSkiTester() { 
     return skiTester; 
    } 

    public void setSkiTester(SkiTester skiTester) { 
     this.skiTester = skiTester; 
    } 

    public Rfid getRfid() { 
     return rfid; 
    } 

    public void setRfid(Rfid rfid) { 
     this.rfid = rfid; 
    } 

    public TestSession getTestSession() { 
     return testSession; 
    } 

    public void setTestSession(TestSession testSession) { 
     this.testSession = testSession; 
    } 

}

PreparationGlideWax.java

@DatabaseTable(tableName = "preparation_glide_wax") 
public class PreparationGlideWax { 

    @DatabaseField(generatedId=true) 
    private int id; 

    @DatabaseField(canBeNull = false) 
    private int layer; 

    @DatabaseField(foreign=true, canBeNull = false) 
    private GlideWax glideWax; 

    @DatabaseField(foreign=true, canBeNull = false) 
    private Preparation preparation; 

    public PreparationGlideWax() { 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public int getLayer() { 
     return layer; 
    } 

    public void setLayer(int layer) { 
     this.layer = layer; 
    } 

    public GlideWax getGlideWax() { 
     return glideWax; 
    } 

    public void setGlideWax(GlideWax glideWax) { 
     this.glideWax = glideWax; 
    } 

    public Preparation getPreparation() { 
     return preparation; 
    } 

    public void setPreparation(Preparation preparation) { 
     this.preparation = preparation; 
    } 

}

+0

我正是這樣解決了我的問題。我會接受你對未來訪客的回答。 – user1411166

0

正如我在編輯中所說的,我可以從父母中加載孩子就好了。我的問題是我需要模型之間的多對多關係。我會在兩天內接受這個答案。