我想用两个foreignCollections来持久化一个对象。但是当我尝试查询对象时,我的foreignId总是为空。我已经读过这个答案了,但它对我没有真正的帮助:Collections in ORMLite
VOPerception perception = new VOPerception();
perception.setOrientation(daoOrientation.createIfNotExists(
orientationLocalizer.getCurrentOrientation()));
ForeignCollection<VOAccessPoint> fAp =
daoPerception.getEmptyForeignCollection("accessPoints");
fAp.addAll(wifiLocalizer.getCurrentScanResultMap());
perception.setAccessPoints(fAp);
daoPerception.create(perception);
List<VOPerception> list = daoPerception.queryForAll();在这里,数据被正确地存储,但是VOAccessPoint对象与父VOPerception对象没有链接。
下面是我的两个类:
public class VOPerception {
@DatabaseField(generatedId=true)
private int per_id;
@ForeignCollectionField(eager=true)
ForeignCollection<VOAccessPoint> accessPoints;
...
}
public class VOAccessPoint{
@DatabaseField(generatedId=true)
private int ap_id;
@DatabaseField(foreign=true,columnName="apForeignPerception_id")
private VOPerception apForeignPerception;
...
}发布于 2011-09-08 01:35:39
您的queryForAll()没有返回任何对象,因为您的VOAccessPoint实例都没有将其apForeignPerception字段设置为perception。使用ForeignCollection添加VOAccessPoint对象会将它们添加到DAO中,但不会自动分配它们的apForeignPerception字段。
你应该这样做:
...
Collection<VOAccessPoint> points = wifiLocalizer.getCurrentScanResultMap();
for (VOAccessPoint point : points) {
point.setApForeignPerception(perception);
}
fAp.addAll(points);
...我可以理解您可能认为这将如何自动处理,但在将它们添加到ForeignCollection时,甚至没有分配perception。我怀疑ORMLite在这里有一个缺失的功能,或者至少是一个更好的例外。
发布于 2014-04-04 04:24:34
我建议使用assignEmptyForeignCollection(Obj parent, fieldName)。这将创建一个新的外部集合,您将通过add(Obj element)添加的所有对象都将自动设置父值。
https://stackoverflow.com/questions/7334901
复制相似问题