使用集合获取 objects

Fetching objects with Collections

我正在尝试列出具有内部集合的 object。我可以很好地保存和检索 objects,但是当我这样做时:

parentRepo.findAll()

只有最后一个 object 列出了 child object,其他的是空集合。

Parent 型号

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

Child 型号

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

eager truefalse 没有任何区别。如果我查询 child 并得到它的 parent,我也可以得到它的 children。我错过了什么?

编辑:

它正在为我制作的模型工作。我的错误是我需要 parent 和 child 之间的 Many-to-many 关系。我进行了快速研究,我需要的是一个中间模型来实现这一点。我将关闭这个问题,并尝试在我的模型之间建立这种 many-to-many 关系。

正如我在编辑中所说,我可以很好地从父项加载子项。我的问题是我的模型之间需要多对多关系。我会在两天后接受这个答案。

我这样解决多对多关系: 这是一个正在进行的项目的例子。我在 Preparation 和 GlideWax 之间存在多对多关系。为了解决这个问题,我使用了 类:Preparation、GlideWax 和 PreparationGlideWax。 PreparationGlideWax 表示另一个 类 之间的连接,就像您通常用 table 解决多对多关系的方式,即 table 之间的 "link"在关系中。如您所见,GripWax 和 Structure 也与准备工作有 Many-to_many 关系。这是代码:

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;
    }

}

准备GlideWax.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;
    }

}