我试图通过SQLLite了解一下有关Android持久性的ORM工具,所以我决定编写一个小测试应用程序。 但是,我似乎无法解决这个问题。我似乎完成了所有工作,例如OrmLite docs的Account,Orders示例。但是,我一直得到同样的错误:
外部集合类com.example.dataobjects.LocationObject用于字段'位置' column-name不包含com.example.dataobjects.TripObject类的外部字段
我试图在LocationObject和TripObject之间建立一对多的关系 (一个TripObject有很多LocationObjects)
LocationObject:
@DatabaseTable
public class LocationObject {
private static final String TRIP_ID_FIELD_NAME = "trip_id";
@DatabaseField(generatedId = true)
private int Id;
@DatabaseField
private double latitude;
@DatabaseField
private double longitude;
@DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = TRIP_ID_FIELD_NAME)
private TripObject trip;
public LocationObject()
{
}
public LocationObject(TripObject trip, double latitude, double longitude) {
this.trip = trip;
this.latitude = latitude;
this.longitude = longitude;
}
@Override
public String toString() {
return "LocationObject [Id=" + Id + ", latitude=" + latitude
+ ", longitude=" + longitude + "]";
}
}
TripObject:
@DatabaseTable
public class TripObject {
@DatabaseField(generatedId = true)
private int Id;
@DatabaseField
private String description;
@DatabaseField
private Date createdAt;
@DatabaseField
private int noOfPotholes;
@DatabaseField
private int noOfSpeedbumps;
@ForeignCollectionField
private ForeignCollection<LocationObject> locations;
public TripObject()
{
}
public TripObject(String description, Date createdAt, int noOfPotholes,
int noOfSpeedbumps) {
super();
this.description = description;
this.createdAt = createdAt;
this.noOfPotholes = noOfPotholes;
this.noOfSpeedbumps = noOfSpeedbumps;
this.createdAt = new Date();
}
public ForeignCollection<LocationObject> getLocations()
{
return this.locations;
}
@Override
public String toString() {
return "TripObject [Id=" + Id + ", description=" + description
+ ", createdAt=" + createdAt + ", noOfPotholes=" + noOfPotholes
+ ", noOfSpeedbumps=" + noOfSpeedbumps + ", locations="
+ locations + "]";
}
}
我想要做的是测试一些插入和查询以了解该工具的工作原理,但我似乎陷入困境。
添加活动:
private DatabaseHelper dbHelper;
private void testAddRetrieve() throws SQLException
{
dbHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
RuntimeExceptionDao<TripObject, Integer> tripDao = dbHelper.getTripRuntimeExceptionDao();
RuntimeExceptionDao<LocationObject, Integer> locationDao = dbHelper.getLocationRuntimeExceptionDao();
TripObject trip = new TripObject("Test Trip 1", new Date(), 12, 2);
tripDao.create(trip);
TripObject queriedTrip = tripDao.queryBuilder().where().eq("Id", 1).queryForFirst();
Toast.makeText(this,"Trip added: " + queriedTrip.toString(), Toast.LENGTH_LONG).show();
System.out.println("Trip added: " + queriedTrip.toString());
// add
locationDao.create(new LocationObject(trip, 25.5,32.4));
locationDao.create(new LocationObject(trip, 23.5,41.4));
locationDao.create(new LocationObject(trip, 12.5,34.4));
// query
List<LocationObject> locations = locationDao.queryForAll();
Toast.makeText(this, "Locations retrieved: " + locations.toString(), Toast.LENGTH_LONG).show();
System.out.println("Locations retrieved: " + locations.toString());
OpenHelperManager.releaseHelper();
}