我正在尝试列出具有内部Collection的对象。我可以很好地保存和检索对象,但是当我这样做时:
parentRepo.findAll()
只有最后一个对象列出了它的子对象,其他对象有一个空集合。
家长模式
@ForeignCollectionField(eager = false)
private Collection<Child> childs;
儿童模型
@DatabaseField(foreign=true,foreignAutoRefresh=true)
private Parent parent;
渴望 true 或 false 没有任何区别。如果我查询一个孩子并得到它的父母,我也可以得到它的孩子。我错过了什么?
编辑:
它正在为我所做的建模而努力。我的错误是我需要父母和孩子之间的多对多关系。我做了一个快速研究,我需要的是一个实现这一目标的中间模型。我将关闭这个问题,并尝试在我的模型之间建立这种多对多的关系。
答案 0 :(得分:2)
我解决了这样的多对多关系: 这是正在进行的项目的一个例子。我在Preparation和GlideWax之间有多对多的关系。为了解决这个问题,我使用了以下类:Preparation,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;
}
}
答案 1 :(得分:0)
正如我在编辑中所说,我能够从父母那里加载孩子就好了。我的问题是我的模型之间需要多对多的关系。我会在两天内接受这个答案。