问题描述
我用ORMLite,试图用ForeignCollectionKey,但我得到了以下错误:
我有我的命名区域对象:
公共类区实现Serializable {
私有静态最后长的serialVersionUID = 1L;
公共静态最后弦乐ZONE_ID =ID;
公共静态最后弦乐ZONE_PARENT_ID =parentZoneId;
@DatabaseField(generatedId =真)
私人诠释身份证;
@DatabaseField()
字符串名称;
@DatabaseField(外资= TRUE,foreignAutoRefresh = TRUE)
区parentZone;
@ForeignCollectionField(foreignFieldName =parentZone,急于= TRUE)
私人ForeignCollection<区> zoneChild;
公共区(){
// TODO自动生成构造函数存根
}
公共ForeignCollection<区> getZoneChild(){
返回zoneChild;
}
公共无效setZoneChild(ForeignCollection<区> zoneChild){
this.zoneChild = zoneChild;
}
公共字符串的getName(){
返回名称;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
在一个类我做一个递归方法来获取所有我区子对象:
公共无效getZone(区地带,道<区域,整数GT; tempZoneDao){
ZoneListEntity zoneEntity =新ZoneListEntity();
zoneEntity.setName(zone.getName());
zoneEntity.setNiveau(0);
zoneEntity.setZone(区);
mainZoneList.add(zoneEntity);
名单<区>的childList =新的ArrayList<区>(zone.getZoneChild());
//设置根区的孩子ZoneListEntity
对于(区currentZone:的childList){
ZoneListEntity zoneGroup =新ZoneListEntity();
zoneGroup.setName(currentZone.getName());
zoneGroup.setZone(currentZone);
的System.out.println(区域:+ currentZone.getName());
getZone(currentZone,tempZoneDao);
}
}
当我进入的第一次在我的 getZone
,一切都很顺利。后来,当我环路 getZone
应用程序崩溃试图访问该子区域:
名单,其中,区>的childList =新的ArrayList<区>(zone.getZoneChild());
你有什么想法?是我的模型建筑吗?谢谢
所以,异常消息是试图解释是怎么回事。我不知道如何可以改善。
您试图访问 zoneChild
这是一个 ForeignCollection
已被反序列化。既然已经反序列化所有基本数据库配置和连接的不能重新建立。我想,当它存储在一个Android 捆绑
会发生这种情况?我不知道,如果这是唯一的案例。
如果您需要获得区
的孩子,你将不得不要么调用 dao.refresh()
对实体的在的你反序列化或做查询自己通过执行 zoneDao
。
I'm using ORMLite, trying to use the ForeignCollectionKey but I got the following error :
I've my object named Zone :
public class Zone implements Serializable {
private static final long serialVersionUID = 1L;
public static final String ZONE_ID = "id";
public static final String ZONE_PARENT_ID = "parentZoneId";
@DatabaseField(generatedId=true)
private int id;
@DatabaseField()
String name;
@DatabaseField(foreign=true, foreignAutoRefresh = true)
Zone parentZone;
@ForeignCollectionField(foreignFieldName = "parentZone", eager = true)
private ForeignCollection<Zone> zoneChild;
public Zone() {
// TODO Auto-generated constructor stub
}
public ForeignCollection<Zone> getZoneChild() {
return zoneChild;
}
public void setZoneChild(ForeignCollection<Zone> zoneChild) {
this.zoneChild = zoneChild;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
In a class i'm doing a recursive method to get all my zone child objects :
public void getZone(Zone zone, Dao<Zone, Integer> tempZoneDao){
ZoneListEntity zoneEntity = new ZoneListEntity();
zoneEntity.setName(zone.getName());
zoneEntity.setNiveau(0);
zoneEntity.setZone(zone);
mainZoneList.add(zoneEntity);
List<Zone> childList = new ArrayList<Zone>(zone.getZoneChild());
//set rootZone's children as ZoneListEntity
for(Zone currentZone : childList){
ZoneListEntity zoneGroup = new ZoneListEntity();
zoneGroup.setName(currentZone.getName());
zoneGroup.setZone(currentZone);
System.out.println("Zone : "+currentZone.getName());
getZone(currentZone, tempZoneDao);
}
}
When i'm entering for the first time in my getZone
, everything going well. Then when I loop in getZone
the application crashes trying to access to the child zone :
List<Zone> childList = new ArrayList<Zone>(zone.getZoneChild());
Do you have any ideas ? Is my model construction right ?Thanks
So the exception message is trying to explain what is going on. I'm not sure how it can be improved.
You are trying to access zoneChild
which is a ForeignCollection
that has been deserialized. Since it has been deserialized all of the underlying database configurations and connections could not be reestablished. I guess this can happen when it stored in an Android Bundle
? I'm not sure if this is the only case.
If you need to get the Zone
children you are going to have to either call dao.refresh()
on the entity after you deserialize it or do the query yourself by doing the zoneDao
.
这篇关于ORMLite:内部DAO对象为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!