gpt4 book ai didi

java - Ormlite - 扩展 BaseDaoImpl 时构造函数调用失败

转载 作者:搜寻专家 更新时间:2023-11-01 02:14:16 24 4
gpt4 key购买 nike

我有以下表格 -

@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
}
}

对于这些表,关联的Dao和DaoImpl如下

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);
}
}

数据库助手如下:

 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;
}
}

现在,当我尝试调用 -

ADao aDao = databaseHelper.getA();

它因以下错误而出错:

 Could not call the constructor in class class A_DaoImpl

现在,如果我在 A 中没有外键 - 即如果 A 不包含 public Bb,它可以正常工作。我在这里缺少什么吗?

非常感谢您。

最佳答案

我怀疑您在异常堆栈跟踪的末尾缺少原因消息。例如,如果我复制上面的示例,我会得到:

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)
...

因为A有一个类B的外部字段,所以B需要有一个id字段。外国字段需要标识字段。

我确定 AB 是您类的简化版本,因此如果您发布更多异常,包括所有原因信息,我将编辑我的适当回答。

关于java - Ormlite - 扩展 BaseDaoImpl 时构造函数调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10184169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com