问题
I have the following tables -
@DatabaseTable(tableName="b", daoClass=B_DaoImpl.class)
public class B {
@DatabaseField
public String b1 ;
public B(){
// For Ormlite
}
}
@DatabaseTable(tableName="a", daoClass=A_DaoImpl.class)
public class A {
@DatabaseField
public String a1 ;
@DatabaseField(foreign=true)
public B b;
public A(){
// For Ormlite
}
}
For these tables, the associated Dao and DaoImpl are as follows
public interface A_Dao extends Dao<A, String>{}
public interface B_Dao extends Dao<B, String>{}
public class B_DaoImpl extends BaseDaoImpl<User, String> implements B_Dao {
public B_DaoImpl(ConnectionSource connectionSource) throws SQLException {
super(connectionSource, B.class);
}
}
public class A_DaoImpl extends BaseDaoImpl<User, String> implements A_Dao {
public A_DaoImpl(ConnectionSource connectionSource) throws SQLException {
super(connectionSource, A.class);
}
}
Database helper is as follows:
public class DatabaseHelperImpl extends OrmLiteSqliteOpenHelper implements DatabaseHelper {
private A_DaoImpl aDao = null;
private B_DaoImpl bDao = null;
public B_DaoImpl getBDao() throws SQLException {
if (bDao == null) {
bDao = getDao(B.class);
}
return bDao;
}
public A_DaoImpl getA() throws SQLException {
if (aDao == null ) {
aDao = getDao(A.class);
}
return aDao;
}
}
Now, when I try to call -
ADao aDao = databaseHelper.getA();
it errors out with the following error:
Could not call the constructor in class class A_DaoImpl
Now, if I do not have the foriegn key in A - ie if A does not contain public B b, it works fine. Is there something that I am missing here?
Thank you very much in advance.
回答1:
I suspect that there is cause message you are missing at the end of your exception stack trace. For example, if I duplicate your example above I get:
java.sql.SQLException: Could not call the constructor in class class
com.j256.ormlite.table.CustomDaoTest$A_DaoImpl
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
...
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
Caused by: java.lang.IllegalArgumentException: Foreign field class
>>>> com.j256.ormlite.table.CustomDaoTest$B does not have id field <<<<<<
at com.j256.ormlite.field.FieldType.configDaoInformation(FieldType.java:332)
...
Because A
has a foreign field of class B
, then B
needs to have an id field. Identity fields are required for foreign fields.
I'm sure A
and B
are simplistic versions of your classes so if you post more of the exception including all of the cause information, I'll edit my answer appropriately.
来源:https://stackoverflow.com/questions/10184169/ormlite-constructor-call-failing-when-basedaoimpl-is-extended