Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ORMLite抽象类的外部集合_Java_Android_Foreign Keys_Abstract Class_Ormlite - Fatal编程技术网

Java ORMLite抽象类的外部集合

Java ORMLite抽象类的外部集合,java,android,foreign-keys,abstract-class,ormlite,Java,Android,Foreign Keys,Abstract Class,Ormlite,我有4门课: @DatabaseTable(tableName = "bucket") public class Bucket { ... @ForeignCollectionField(eager = true) private Collection<Good> goods; ... } public abstract class Good { ... @DatabaseField(foreign = true, foreignAu

我有4门课:

@DatabaseTable(tableName = "bucket")
public class Bucket {
    ...
    @ForeignCollectionField(eager = true)
    private Collection<Good> goods;
    ...
}

public abstract class Good {
    ...
    @DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
    private Bucket bucket;
    ...
}

@DatabaseTable(tableName = "bread")
public class Bread extends Good {
    ...
}

@DatabaseTable(tableName = "milk")
public class Milk extends Good {
    ...
}
有办法解决这个问题吗?

或者我甚至不能创建抽象类的ForeignCollectionField?

我发现解决这个问题非常简单

我刚刚从
Bucket
模型中删除了
@ForeignCollectionField
。所以,在
Bucket
表中,ORMLite对
好的
实体一无所知

持久化数据:

// 1 using bucketDao add bucket
bucketDao.add(bucket)
// 2 iterate over collection
for (Bread bread: breadList) {
  bread.setBucket(bucket); // set current bucket to good item
  breadDao.add(bread); // using breadDao add bread
}
// 3 do the same with milk collection
for (Milk milk: milkList) {
  milk.setBucket(bucket);
  milkDao.add(milk);
}
int bucketId = 1; // for example working with bucket with id=1
Bucket bucket = bucketDao.queryForEq("id", bucketId)); // query for this bucket
List<Milk> milkList = milkDao.queryForEq("bucket_id", bucketId); // query for all milks, that has a foreign key to current bucket
List<Break> breadList = breadDao.queryForEq("bucket_id", bucketId); // the same for bread

// finally set milks and breads to Good collection inside Bucket
List<Good> goodList = new ArrayList<>();   
goodList.addAll(milkList);   
goodList.addAll(breadList);   
bucket.setGoods(goodList);
加载数据:

// 1 using bucketDao add bucket
bucketDao.add(bucket)
// 2 iterate over collection
for (Bread bread: breadList) {
  bread.setBucket(bucket); // set current bucket to good item
  breadDao.add(bread); // using breadDao add bread
}
// 3 do the same with milk collection
for (Milk milk: milkList) {
  milk.setBucket(bucket);
  milkDao.add(milk);
}
int bucketId = 1; // for example working with bucket with id=1
Bucket bucket = bucketDao.queryForEq("id", bucketId)); // query for this bucket
List<Milk> milkList = milkDao.queryForEq("bucket_id", bucketId); // query for all milks, that has a foreign key to current bucket
List<Break> breadList = breadDao.queryForEq("bucket_id", bucketId); // the same for bread

// finally set milks and breads to Good collection inside Bucket
List<Good> goodList = new ArrayList<>();   
goodList.addAll(milkList);   
goodList.addAll(breadList);   
bucket.setGoods(goodList);
int-bucketId=1;//例如,使用id为1的铲斗
Bucket Bucket=bucketDao.queryForEq(“id”,bucketId));//查询此桶
List milkList=milkDao.queryForEq(“bucket_id”,bucketId);//查询当前存储桶具有外键的所有牛奶
List breadList=breadDao.queryForEq(“bucket_id”,bucketId);//面包也一样
//最后把牛奶和面包放在桶里收集好
List goodList=new ArrayList();
goodList.addAll(milkList);
goodList.addAll(面包列表);
桶装货物(货物清单);