首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何链接我的getEmptyForeignCollection()对象和我的父对象?

如何链接我的getEmptyForeignCollection()对象和我的父对象?
EN

Stack Overflow用户
提问于 2011-09-07 21:44:31
回答 2查看 944关注 0票数 1

我想用两个foreignCollections来持久化一个对象。但是当我尝试查询对象时,我的foreignId总是为空。我已经读过这个答案了,但它对我没有真正的帮助:Collections in ORMLite

代码语言:javascript
复制
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对象没有链接。

下面是我的两个类:

代码语言:javascript
复制
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;
    ...
}
EN

回答 2

Stack Overflow用户

发布于 2011-09-08 01:35:39

您的queryForAll()没有返回任何对象,因为您的VOAccessPoint实例都没有将其apForeignPerception字段设置为perception。使用ForeignCollection添加VOAccessPoint对象会将它们添加到DAO中,但不会自动分配它们的apForeignPerception字段。

你应该这样做:

代码语言:javascript
复制
...
Collection<VOAccessPoint> points = wifiLocalizer.getCurrentScanResultMap();
for (VOAccessPoint point : points) {
    point.setApForeignPerception(perception);
}
fAp.addAll(points);
...

我可以理解您可能认为这将如何自动处理,但在将它们添加到ForeignCollection时,甚至没有分配perception。我怀疑ORMLite在这里有一个缺失的功能,或者至少是一个更好的例外。

票数 1
EN

Stack Overflow用户

发布于 2014-04-04 04:24:34

我建议使用assignEmptyForeignCollection(Obj parent, fieldName)。这将创建一个新的外部集合,您将通过add(Obj element)添加的所有对象都将自动设置父值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7334901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档