我无法从 table 获取值。关系一对多
I can not get values from table. Relationship one to many
您好,我在从数据库中获取许多记录时遇到了问题
这是我的主要 table
@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;
}
}
这是第二个table
@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);
}
这部分总是return null
ForeignCollection<PortTable> mPortsForOneTrip = mOneTrip.getPorts();
你能帮帮我吗?哪里出错了?
感谢您的回复!
基本上,您的主要方法应该在 运行 上面的代码中给出以下错误。我不确定为什么 运行 首先是给你的。
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
上面的错误在你的 class MyTripTable.java 中说,你在集合上标记了 @DatabaseField(foreign = true, foreignAutoRefresh = true ),而根据文档你应该使用注释 @外国集合字段。我正在发布 运行 代码。
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;
}
}
端口代码 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);
}
我测试了上面的代码,运行 没问题。如果您遇到任何其他问题,请告诉我。
您好,我在从数据库中获取许多记录时遇到了问题 这是我的主要 table
@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;
}
}
这是第二个table
@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);
}
这部分总是return null
ForeignCollection<PortTable> mPortsForOneTrip = mOneTrip.getPorts();
你能帮帮我吗?哪里出错了? 感谢您的回复!
基本上,您的主要方法应该在 运行 上面的代码中给出以下错误。我不确定为什么 运行 首先是给你的。
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
上面的错误在你的 class MyTripTable.java 中说,你在集合上标记了 @DatabaseField(foreign = true, foreignAutoRefresh = true ),而根据文档你应该使用注释 @外国集合字段。我正在发布 运行 代码。
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;
}
}
端口代码 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);
}
我测试了上面的代码,运行 没问题。如果您遇到任何其他问题,请告诉我。