我正在创建一个可以在本地保存数据的 android 应用程序。我使用过 ormlite 来持久化数据
这是我的模型类结构
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;
导入 java.io.Serializable;
导入 com.j256.ormlite.field.DatabaseField;
公共类参数实现可序列化{
/**
*
*/
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
#
dataClass=com.jbmdigital.model.Site tableName=site
--table-fields-start--
--现场开始--
fieldName=id 生成Id=true
--现场结束--
--现场开始--
fieldName=siteRecordId
--现场结束--
--现场开始--
fieldName=recorderId
--现场结束--
--现场开始--
字段名称=站点名称索引名称=
--现场结束--
--现场开始--
fieldName=siteDatum
--现场结束--
--现场开始--
fieldName=siteEasting
--现场结束--
--现场开始--
字段名=站点纬度
--现场结束--
--现场开始--
字段名=站点经度
--现场结束--
--现场开始--
fieldName=siteNorthing
--现场结束--
--现场开始--
fieldName=siteLocationMethod
--现场结束--
--现场开始--
fieldName=siteZone
--现场结束--
--现场开始--
字段名=站点类型
--现场结束--
--现场开始--
fieldName=horizantalAccuracy
--field-end-
--现场开始--
fieldName=landFormPattern
--现场结束--
--现场开始--
fieldName=landFormElement
--现场结束--
--现场开始--
fieldName=vagation
--现场结束--
--现场开始--
字段名=土地用途
--现场结束--
--现场开始--
fieldName=距离到水
--现场结束--
--现场开始--
fieldName=howToSite
--现场结束--
--现场开始--
fieldName=primaryReport
--现场结束--
--table-fields-end--
--表端--
--table-start--
dataClass=com.jbmdigital.model.Parameter 表名=参数
--table-fields-start--
--现场开始--
fieldName=id 生成Id=true
--现场结束--
--现场开始--
字段名=站点 ID
--现场结束--
--现场开始--
fieldName=categoryId
--现场结束--
--现场开始--
字段名=参数值
--现场结束--
--现场开始--
fieldName=网站外=真
--现场结束--
--现场开始--
字段名=可克隆
--现场结束--
--table-fields-end--
--表端--
#这是我的活动课
------------------------------------------------------------------------
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。