无法使用ORM-LITE android加载一对多的数据

时间:2013-05-27 07:42:23

标签: android ormlite

我正在创建一个可以在本地保存数据的Android应用程序。我用orm-lite来保存数据

这是我的模型类结构

public class Site implements Serializable {

    private static final long serialVersionUID = 1L;

    @DatabaseField(generatedId = true)
    int id;
    @DatabaseField
    int siteRecordId;
    @DatabaseField
    int recorderId;
    @DatabaseField
    String siteName;
    @DatabaseField
    String siteDatum;
    @DatabaseField
    String siteEasting;
    @DatabaseField
    String siteLatitude;
    @DatabaseField
    String siteLongtitude;
    @DatabaseField
    String siteNorthing;
    @DatabaseField
    String siteLocationMethod;
    @DatabaseField
    String siteZone;
    @DatabaseField
    String siteType;
    @DatabaseField
    String horizantalAccuracy;
    @DatabaseField
    String landFormPattern;
    @DatabaseField
    String landFormElement;
    @DatabaseField
    String vagetation;
    @DatabaseField
    String landUse;
    @DatabaseField
    String distanceToWater;
    @DatabaseField
    String howToSite;
    @DatabaseField
    String primaryReport;
    @ForeignCollectionField(eager = true)
    ForeignCollection<Parameter> parameters;


    public Site(){

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getSiteRecordId() {
        return siteRecordId;
    }

    public void setSiteRecordId(int siteRecordId) {
        this.siteRecordId = siteRecordId;
    }

    public int getRecorderId() {
        return recorderId;
    }

    public void setRecorderId(int recorderId) {
        this.recorderId = recorderId;
    }

    public String getSiteName() {
        return siteName;
    }

    public void setSiteName(String siteName) {
        this.siteName = siteName;
    }

    public String getSiteDatum() {
        return siteDatum;
    }

    public void setSiteDatum(String siteDatum) {
        this.siteDatum = siteDatum;
    }

    public String getSiteEasting() {
        return siteEasting;
    }

    public void setSiteEasting(String siteEasting) {
        this.siteEasting = siteEasting;
    }

    public String getSiteLatitude() {
        return siteLatitude;
    }

    public void setSiteLatitude(String siteLatitude) {
        this.siteLatitude = siteLatitude;
    }

    public String getSiteLongtitude() {
        return siteLongtitude;
    }

    public void setSiteLongtitude(String siteLongtitude) {
        this.siteLongtitude = siteLongtitude;
    }

    public String getSiteNorthing() {
        return siteNorthing;
    }

    public void setSiteNorthing(String siteNorthing) {
        this.siteNorthing = siteNorthing;
    }

    public String getSiteLocationMethod() {
        return siteLocationMethod;
    }

    public void setSiteLocationMethod(String siteLocationMethod) {
        this.siteLocationMethod = siteLocationMethod;
    }

    public String getSiteZone() {
        return siteZone;
    }

    public void setSiteZone(String siteZone) {
        this.siteZone = siteZone;
    }

    public String getSiteType() {
        return siteType;
    }

    public void setSiteType(String siteType) {
        this.siteType = siteType;
    }

    public String getHorizantalAccuracy() {
        return horizantalAccuracy;
    }

    public void setHorizantalAccuracy(String horizantalAccuracy) {
        this.horizantalAccuracy = horizantalAccuracy;
    }

    public String getLandFormPattern() {
        return landFormPattern;
    }

    public void setLandFormPattern(String landFormPattern) {
        this.landFormPattern = landFormPattern;
    }

    public String getLandFormElement() {
        return landFormElement;
    }

    public void setLandFormElement(String landFormElement) {
        this.landFormElement = landFormElement;
    }

    public String getVagetation() {
        return vagetation;
    }

    public void setVagetation(String vagetation) {
        this.vagetation = vagetation;
    }

    public String getLandUse() {
        return landUse;
    }

    public void setLandUse(String landUse) {
        this.landUse = landUse;
    }

    public String getDistanceToWater() {
        return distanceToWater;
    }

    public void setDistanceToWater(String distanceToWater) {
        this.distanceToWater = distanceToWater;
    }

    public String getHowToSite() {
        return howToSite;
    }

    public void setHowToSite(String howToSite) {
        this.howToSite = howToSite;
    }

    public String getPrimaryReport() {
        return primaryReport;
    }

    public void setPrimaryReport(String primaryReport) {
        this.primaryReport = primaryReport;
    }

    public ForeignCollection<Parameter> getParameters() {
        return parameters;
    }

    public void setParameters(ForeignCollection<Parameter> parameters) {
        this.parameters = parameters;
    }



}


package com.jbmdigital.model;

import java.io.Serializable;

import com.j256.ormlite.field.DatabaseField;

public class Parameter实现Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
@DatabaseField(generatedId = true)
int id;
@DatabaseField
int  categoryId;
@DatabaseField
int siteId;
@DatabaseField
String paramValue;
@DatabaseField
int clonable;
@DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = "site")
    Site site;


public Parameter(){

}

public Parameter(int categoryId,String paramValue,int clonable,Site site){
    this.categoryId = categoryId;
    this.paramValue = paramValue;
    this.clonable = clonable;
    this.site = site;
}

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public int getCategoryId() {
    return categoryId;
}
public void setCategoryId(int categoryId) {
    this.categoryId = categoryId;
}
public String getParamValue() {
    return paramValue;
}
public void setParamValue(String paramValue) {
    this.paramValue = paramValue;
}
public int getClonable() {
    return clonable;
}
public void setClonable(int clonable) {
    this.clonable = clonable;
}
public Site getSite() {
    return site;
}
public void setSite(Site site) {
    this.site = site;
}

}

这是我的ormlite_config.txt

于2013/05/09 09:13:AM

生成

数据类= com.jbmdigital.model.Site 表名=站点

- 表场启动 -

- 场 - 起动 -

的fieldName = ID generatedId =真

- 场 - 端 -

- 场 - 起动 -

的fieldName = siteRecordId

- 场 - 端 -

- 场 - 起动 -

的fieldName = recorderId

- 场 - 端 -

- 场 - 起动 -

的fieldName = SITENAME INDEXNAME =

- 场 - 端 -

- 场 - 起动 -

的fieldName = siteDatum

- 场 - 端 -

- 场 - 起动 -

的fieldName = siteEasting

- 场 - 端 -

- 场 - 起动 -

的fieldName = siteLatitude

- 场 - 端 -

- 场 - 起动 -

的fieldName = siteLongtitude

- 场 - 端 -

- 场 - 起动 -

的fieldName = siteNorthing

- 场 - 端 -

- 场 - 起动 -

的fieldName = siteLocationMethod

- 场 - 端 -

- 场 - 起动 -

的fieldName = siteZone

- 场 - 端 -

- 场 - 起动 -

的fieldName = siteType

- 场 - 端 -

- 场 - 起动 -

的fieldName = horizantalAccuracy

- 场 - 端 -

- 场 - 起动 -

的fieldName = landFormPattern

- 场 - 端 -

- 场 - 起动 -

的fieldName = landFormElement

- 场 - 端 -

- 场 - 起动 -

的fieldName = vagetation

- 场 - 端 -

- 场 - 起动 -

的fieldName =土地利用

- 场 - 端 -

- 场 - 起动 -

的fieldName = distanceToWater

- 场 - 端 -

- 场 - 起动 -

的fieldName = howToSite

- 场 - 端 -

- 场 - 起动 -

的fieldName = primaryReport

- 场 - 端 -

- 表场端 -

- 表端 -

- 表 - 起动 -

数据类= com.jbmdigital.model.Parameter 表名=参数

- 表场启动 -

- 场 - 起动 -

的fieldName = ID generatedId =真

- 场 - 端 -

- 场 - 起动 -

的fieldName =网站ID

- 场 - 端 -

- 场 - 起动 -

的fieldName =的categoryId

- 场 - 端 -

- 场 - 起动 -

的fieldName = paramValue

- 场 - 端 -

- 场 - 起动 -

的fieldName =站点 外国=真

- 场 - 端 -

- 场 - 起动 -

的fieldName =可克隆

- 场 - 端 -

- 表场端 -

- 表端 -

这是我的Activity类

------------------------------------------------------------------------



DatabaseHelper db = new DatabaseHelper(this);
                RuntimeExceptionDao<Site, Integer> simpleSiteDao = db.getSimpleSiteDataDao();
                RuntimeExceptionDao<Parameter, Integer> simpleParamDao = db.getSimpleaParameterDataDao();
                simpleSiteDao.create(site);

                Site accountResult = simpleSiteDao.queryForId(site.getId());

                for (Parameter parameter : parameterList) {
                    parameter.setSite(accountResult);
                    simpleParamDao.create(parameter);

                }
                List<Site> sites= simpleSiteDao.queryForAll();

                int index = sites.size()-1;
                 Log.d("count",""+sites.get(index).getParameters().size());

但如果我试图打印日志,它会给我零点异常。我想我不会根据Site实例获取ParameterList。

0 个答案:

没有答案