我有从db获取许多记录的问题 这是我的主要表格
@DatabaseTable(tableName = "MyTrips")
public class MyTripTable {
@DatabaseField(generatedId = true)
private long id;
@DatabaseField
private long timestamp;
@DatabaseField(foreign = true, foreignAutoRefresh = true )
private ForeignCollection<PortTable> mPorts;
public MyTripTable() {}
public ForeignCollection<PortTable> getPorts() {
return mPorts;
}
public long getId() {
return id;
}
}
这是第二个表
@DatabaseTable(tableName = "Ports")
public class PortTable {
@DatabaseField(generatedId = true)
private long id;
@DatabaseField
private long timestamp;
@DatabaseField(foreign = true, foreignAutoRefresh = true )
private MyTripTable myTripTable;
public PortTable() {}
public void setTripAssigned(MyTripTable myTripTable){
this.myTripTable = myTripTable;
}
}
我需要一个MyTrips
有更多Ports
我这样做是example但不起作用
问题在这里:
private void strawberry(){
DatabaseHelper helper = OpenHelperManager.getHelper(getApplicationContext(), DatabaseHelper.class);
RuntimeExceptionDao<MyTripTable, Long> mTrip = helper.getTripRuntimeExcaptionDao();
RuntimeExceptionDao<PortTable, Long> mPort = helper.getPortRuntimeExcaptionDao();
long time1 = Calendar.getInstance().getTimeInMillis();
//crate data
MyTripTable myFirstTrip = new MyTripTable(153315, "Praha - Gothenburg", "Big fat woman",
time1,time1,time1,time1,1,5,6 );
mTrip.create(myFirstTrip);
PortTable port1 = new PortTable("Praha",
"Praha is the capital and largest city of the Czech Republic. ",
50.078047,
14.427172);
port1.setTripAssigned(myFirstTrip);
PortTable port2 = new PortTable("Praha",
"Praha is the capital and largest city of the Czech Republic. ",
50.078047,
14.427172);
port2.setTripAssigned(myFirstTrip);
PortTable port3 = new PortTable("Praha",
"Praha is the capital and largest city of the Czech Republic. ",
50.078047,
14.427172);
port3.setTripAssigned(myFirstTrip);
PortTable port4 = new PortTable("Praha",
"Praha is the capital and largest city of the Czech Republic. ",
50.078047,
14.427172);
port4.setTripAssigned(myFirstTrip);
mPort.create(port1);
mPort.create(port2);
mPort.create(port3);
mPort.create(port4);
//Get data
Log.e("LSA", "----------------------------------TRIPS");
List<MyTripTable> alltrips = mTrip.queryForAll();
Log.e("LSA", "TRIPS" + alltrips.toString());
for (MyTripTable trip : alltrips){
Log.e("LSA", "Trip: " + trip.toString());
}
Log.e("LSA", "----------------------------------PORTS");
List<PortTable> allPorts = mPort.queryForAll();
Log.e("LSA", "PORTS" + allPorts.toString());
for (PortTable port : allPorts){
Log.e("LSA", "port: " + port.toString());
}
Log.e("LSA", "----------------------------------PORTs assigned");
MyTripTable mOneTrip = mTrip.queryForId(myFirstTrip.getId());
ForeignCollection<PortTable> mPortsForOneTrip = mOneTrip.getPorts();
Log.e("LSA", "ports inside trip: " + mPortsForOneTrip);
}
此部分始终返回null
ForeignCollection<PortTable> mPortsForOneTrip = mOneTrip.getPorts();
你能帮帮我吗?哪里出错?
谢谢你的回复!
答案 0 :(得分:0)
基本上,您的主要方法应该是在运行上述代码时给出以下错误。我不知道为什么它一开始就适合你。
Exception in thread "main" java.sql.SQLException: Field 'mPorts' in class interface com.j256.ormlite.dao.ForeignCollection' should use the @ForeignCollectionField annotation not foreign=true
上面的错误在您的类MyTripTable.java中说明,您在集合上标记了@DatabaseField(foreign = true,foreignAutoRefresh = true),而根据文档,您应该使用注释@ForeignCollectionField。我发布了正在运行的代码。
MyTripTable.java的代码
@DatabaseTable(tableName = "MyTrips")
public class MyTripTable {
@DatabaseField(generatedId = true)
private long id;
@DatabaseField
private long timestamp;
// @DatabaseField(foreignAutoRefresh = true)
@ForeignCollectionField(eager = false)
private ForeignCollection<PortTable> mPorts;
public MyTripTable() {
}
public ForeignCollection<PortTable> getPorts() {
return mPorts;
}
public long getId() {
return id;
}
public long getTimestamp() {
return timestamp;
}
public void setId(long id) {
this.id = id;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}
Port Table.java的代码
@DatabaseTable(tableName = "Ports")
public class PortTable {
/** The id. */
@DatabaseField(generatedId = true)
private long id;
/** The timestamp. */
@DatabaseField
private long timestamp;
/** The my trip table. */
@DatabaseField(foreign = true, foreignAutoRefresh = true)
private MyTripTable myTripTable;
/**
* Instantiates a new port table.
*/
public PortTable() {
}
/**
* Sets the trip assigned.
*
* @param myTripTable the new trip assigned
*/
public void setTripAssigned(MyTripTable myTripTable) {
this.myTripTable = myTripTable;
}
/**
* Gets the id.
*
* @return the id
*/
public long getId() {
return id;
}
/**
* Gets the timestamp.
*
* @return the timestamp
*/
public long getTimestamp() {
return timestamp;
}
/**
* Sets the id.
*
* @param id the new id
*/
public void setId(long id) {
this.id = id;
}
/**
* Sets the timestamp.
*
* @param timestamp the new timestamp
*/
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}
下面显示了上述代码的执行....
public static void main(String[] args) throws SQLException {
String databaseUrl = "jdbc:mysql://localhost:3306/YOUR_DATABASE";
ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl, "root", "root");
// instantiate the dao
Dao<MyTripTable, Long> tripDao = DaoManager.createDao(connectionSource, MyTripTable.class);
Dao<PortTable, Long> portDao = DaoManager.createDao(connectionSource, PortTable.class);
// if you need to create the 'accounts' table make this call
TableUtils.createTable(connectionSource, MyTripTable.class);
TableUtils.createTable(connectionSource, PortTable.class);
long time1 = Calendar.getInstance().getTimeInMillis();
// crate data
MyTripTable myFirstTrip = new MyTripTable();
myFirstTrip.setTimestamp(time1);
tripDao.create(myFirstTrip);
PortTable port1 = new PortTable();
port1.setTimestamp(time1);
port1.setTripAssigned(myFirstTrip);
PortTable port2 = new PortTable();
port2.setTimestamp(time1);
port2.setTripAssigned(myFirstTrip);
PortTable port3 = new PortTable();
port3.setTimestamp(time1);
port3.setTripAssigned(myFirstTrip);
portDao.create(port1);
portDao.create(port2);
portDao.create(port3);
List<MyTripTable> alltrips = tripDao.queryForAll();
for (MyTripTable trip : alltrips) {
System.out.println(trip);
}
List<PortTable> allPorts = portDao.queryForAll();
for (PortTable port : allPorts) {
System.out.println(port);
}
MyTripTable mOneTrip = tripDao.queryForId(myFirstTrip.getId());
ForeignCollection<PortTable> mPortsForOneTrip = mOneTrip.getPorts();
System.out.println(mPortsForOneTrip);
}
我测试了上面的代码,运行正常。如果您有任何进一步的问题,请告诉我。