ORMLite Foreign Collection of abstract class

I have 4 classes:

@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 {
    ...
}

So, inside Bucket I have ForeignCollectionField of abstract class Good , which, of course, doesn't have its own database table.

Database entries are created normally, but when i try to call queryForAll() it gives me error:

bucketDao.queryForAll(); 
// no such table: good (code 1): , while compiling: SELECT * FROM `basecard` WHERE `bucket_id` = ?

Is there a way to solve this problem?
Or I can't even create ForeignCollectionField of abstract class?


I found it pretty simple to solve this problem.

I just removed @ForeignCollectionField from Bucket model at all. So, Inside Bucket table ORMLite knows nothing about Good entity.

Persist data:

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

Load data:

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);
链接地址: http://www.djcxy.com/p/68670.html

上一篇: Xcode 4.6中的“po”命令第一次非常慢

下一篇: ORMLite抽象类的外部集合