Ormlite - 扩展BaseDaoImpl时构造函数调用失败 [英] Ormlite - Constructor call failing when BaseDaoImpl is extended

查看:145
本文介绍了Ormlite - 扩展BaseDaoImpl时构造函数调用失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表格 -

@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如下所示

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

数据库助手如下:

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

出错时出现以下错误:

 Could not call the constructor in class class A_DaoImpl

现在,如果我没有A中的foriegn键 - 即如果A不包含 public B b ,它就可以正常工作。我在这里找不到什么东西?

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?

提前非常感谢。

推荐答案

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

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

因为 A 有一个外国字段 B ,那么 B 需要有一个id字段。外国字段需要标识字段。

Because A has a foreign field of class B, then B needs to have an id field. Identity fields are required for foreign fields.

我确定 A B 是您的类的简化版本,所以如果您发布更多的异常,包括所有原因信息,我会适当地编辑我的答案。

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.

这篇关于Ormlite - 扩展BaseDaoImpl时构造函数调用失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆