Android 与单个类有一对多关系的
我猜我有一个名为Android 与单个类有一对多关系的,android,ormlite,Android,Ormlite,我猜我有一个名为Many的类。还有另外两个类One1和One2,它们有许多的集合: 第一类1: public class One1 { // ... @ForeignCollectionField(eager = false) private ForeignCollection<Many> events; } 公共一级1{ // ... @ForeignCollectionField(eager=false) 私人募捐活动; } 第二类: public c
Many
的类。还有另外两个类One1
和One2
,它们有许多的集合:
第一类1:
public class One1 {
// ...
@ForeignCollectionField(eager = false)
private ForeignCollection<Many> events;
}
公共一级1{
// ...
@ForeignCollectionField(eager=false)
私人募捐活动;
}
第二类:
public class One2 {
// ...
@ForeignCollectionField(eager = false)
private ForeignCollection<Many> events;
}
公共一级2{
// ...
@ForeignCollectionField(eager=false)
私人募捐活动;
}
Many
实体一次只有一个关系。我的问题是:我应该如何在Many
方面管理这种关系
编辑:
接下来是我的主意Many
类有2个外来
字段,并通过setter控制它们的填充One1
和One2
使用foreignFieldName
属性引用Many
中的不同字段。见下文:
许多
public class Many {
@DatabaseField(foreign = true)
private One1 mOne1;
@DatabaseField(foreign = true)
private One2 mOne2;
public void setOne1(One1 one1) {
mOne1 = one1;
mOne2 = null;
}
public void setOne2(One2 one2) {
mOne2 = one2;
mOne1 = null;
}
}
One1
public class One1 {
@ForeignCollectionField(eager = false, foreignFieldName = "mOne1")
private ForeignCollection<Many> mEvents;
public ForeignCollection<Many> getEvents() {
return mEvents;
}
}
公共一级1{
@ForeignCollectionField(eager=false,foreignFieldName=“mOne1”)
私人外汇基金;
公共ForeignCollection getEvents(){
返回事件;
}
}
One2
public class One2 {
@ForeignCollectionField(eager = false, foreignFieldName = "mOne2")
private ForeignCollection<Many> mEvents;
public ForeignCollection<Many> getEvents() {
return mEvents;
}
}
公共一级2{
@ForeignCollectionField(eager=false,foreignFieldName=“mOne2”)
私人外汇基金;
公共ForeignCollection getEvents(){
返回事件;
}
}
“许多实体一次只有一个关系”。这是否意味着Many
可能与One1
或One2
有关系?而且永远不会同时做这两件事?是的。多侧中的P_ID持有一个1或一个2的ID