【问题标题】:Implementing a base dao, I get error Type of the parameter must be a class annotated with @Entity or a collection/array of it实现一个基本 dao,我得到错误参数类型必须是一个用 @Entity 注释的类或其集合/数组
【发布时间】:2019-12-02 13:22:22
【问题描述】:

我正在努力使用 Android 中的 Room 在 Java 中创建基础 DAO。 那里有几篇帖子,但没有一篇能解决我得到的错误。

这是我在编译时遇到的错误:

error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.

这是我的实体/模型类:

@Entity (tableName = "user")
public class User {

    @PrimaryKey
    @ColumnInfo (name = "user_id")
    private int userId;
    @ColumnInfo (name = "lastname")
    private String lastName;
    @ColumnInfo (name = "firstname")
    private String firstName;

    public User(int userId, String lastName, String firstName) {
        this.userId = userId;
        this.lastName = lastName;
        this.firstName = firstName;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}

这是我的基础 DAO:

@Dao
public abstract class BaseDao<T> {

    @Insert
    public abstract long insert(T object);      // error here

    @Insert
    public abstract long[] insertAll(List<T> object);    // error here

    @Update
    public abstract int update(T object);   // error here

    @Update
    public abstract int updateAll(List<T> object);   // error here

    @Delete
    public abstract int delete(T object);   // error here

    @Delete
    public abstract int deleteAll(List<T> object);    // error here

}

这是我的用户 DAO:


@Dao
public abstract class UserDao extends BaseDao<User> {

    @Query ("select * from user")
    public abstract LiveData<List<User>> getAll();

    @Query("delete from user")
    public abstract int deleteAll();

}

我得到六个相同类型的编译错误。这就是我的基础 DAO 中的函数数量。当然泛型类型T没有用@Entity注解,但是这个事实怎么处理呢?

我试图解决这个问题:

  • 仔细阅读有关此主题的所有帖子。大多数帖子都使用 Kotlin,我相信它适用于 Kotlin,但我使用的是 Java。
  • 试图将 Base DAO 实现为接口或抽象类
  • 尝试使用 @Dao 注释/不注释基本 DAO(如某些帖子中所述)
  • 尝试创建一个使用 @Entity 注释的基本实体,并让我的模型类像这样扩展:
@Entity
public class BaseEntity { ... }    // Base entity annotated with @Entity

public class User extends BaseEntity { ... }    // subclass the model class from BaseEntity

public abstract class BaseDao<T extends BaseEntity> { ...}    // type parameterize BaseDao with base type BaseEntity

public abstract class UserDao extends BaseDao<User> { ...}    // type T should now be an descendant of BaseEntity which is an @Entity 

这些都不适合我!

有些帖子告诉我们,这种方式对他们有用,但对我无效。 我希望有人能告诉我我做错了什么!

【问题讨论】:

  • 哪一行抛出该错误?
  • stackoverflow.com/a/49346991/891373 问题似乎与用@Dao注释基类有关
  • 尝试只使用接口而不是类,你永远不需要@Dao 的对象实例但我不确定这会解决它,我可以给你一个我的@Dao 的例子,但在 Kotlin代码
  • BaseDao 中发生编译错误(我现在在上面的代码中对此进行了注释)。在 BaseDao 类中省略 @Dao 注释并不能解决问题。已经尝试将 BaseDao 和后代实现为接口(没有解决)。我知道,在 Kotlin 中它似乎可以工作......
  • 可能与List&lt;T&gt;有关。如果没有这些方法,可能会尝试一次。如果有效,请尝试将其替换为可变参数。也许这行得通。

标签: java android entity persistence dao


【解决方案1】:

终于找到错误了!

在我的模型类中,我有一个模型,它只是用于连接查询结果的 DTO。

public class SessionAndUser {

    @Embedded
    private Session session;

    @Relation(parentColumn = "user_id", entityColumn = "user_id")
    private User user;
}

DAO 实现如下所示:


@Dao
public abstract class SessionAndUserDao {   //extends BaseDao<SessionAndUser> { <-- this caused the error

    @Transaction
    @Query("select * from session")
    public abstract LiveData<List<SessionandUser>> getAll();

}

由于 SessionAndUser 不在数据库中,所以不能用@Entity 注释。

错误是,我从我的基础 DAO 扩展了 DAO。所以我的解决方案是,只创建 dao 而不扩展基本 DAO(实际上它不需要基本 DAO 的功能)。

弄清楚这一点后,我可以看到每种测试的实现(使用接口或抽象类,使用 Java 或 Kotlin)都按预期工作。

【讨论】:

    猜你喜欢
    • 2022-12-15
    • 2020-09-29
    • 2021-12-29
    • 2018-07-05
    • 2019-12-29
    • 2021-08-27
    • 2022-01-11
    • 1970-01-01
    • 2018-06-09
    相关资源
    最近更新 更多