Room:使用 class 将 ArrayList 扩展为实体
Room: Use class that extends ArrayList as Entity
我正在使用 Room Persistence Library 制作一个 Android 应用程序。我有一个 class extends
ArrayList
class 像这样:
@Entity(tableName = "the_table_name")
public class MyDemoClass<E extends AnotherDemoClass> extends ArrayList<E> {
@PrimaryKey
@NonNull
@ColumnInfo(name = "id")
private String id;
@ColumnInfo(name = "title")
private String title;
@ColumnInfo(name = "creationdate")
private Date creationDate;
// Other stuff
}
我正在尝试添加此 class,它只允许 class 将 extend AnotherDemoClass 作为 Room 的实体包含在其中但是出现编译时错误,即:
error: cannot find symbol
_result = new MyDemoClass<E>();
^
symbol: class E
location: class MyDemoClassDao_Impl
我已经尝试了一个多小时来解决这个问题,但没有成功。
编辑:
这是我的 Dao
:
@Dao
public interface MyDemoClassDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(MyDemoClass<AnotherDemoClass> myDemoClass);
}
如果您的回答有任何帮助,请与我分享!
I'd like to use MyDemoClass as a playlist. I'd add Songs to this playlist and save it inside the database
MyDemoClass
将是一个普通的 ol' Java object (POJO),并且具有与播放列表的简单属性相对应的字段,例如 String
这将作为用户可以提供并在播放列表名册中看到的名称。 MyDemoClass
不会 是 ArrayList
的子类。
Song
将是另一个 POJO,并且具有对应于歌曲的简单属性的字段,例如 String
的标题。
由于一个播放列表可以有多首歌曲,并且一首歌曲可以出现在多个播放列表中,因此您需要创建一个实体来表示该连接,并将外键关系返回到播放列表和歌曲实体。
the Room documentation. It should be covered in any book that spends significant time on Room. For example, here is a preview edition of my chapter on M:N relations in Room (from this book).
中简要介绍了外键关系的使用
我正在使用 Room Persistence Library 制作一个 Android 应用程序。我有一个 class extends
ArrayList
class 像这样:
@Entity(tableName = "the_table_name")
public class MyDemoClass<E extends AnotherDemoClass> extends ArrayList<E> {
@PrimaryKey
@NonNull
@ColumnInfo(name = "id")
private String id;
@ColumnInfo(name = "title")
private String title;
@ColumnInfo(name = "creationdate")
private Date creationDate;
// Other stuff
}
我正在尝试添加此 class,它只允许 class 将 extend AnotherDemoClass 作为 Room 的实体包含在其中但是出现编译时错误,即:
error: cannot find symbol
_result = new MyDemoClass<E>();
^
symbol: class E
location: class MyDemoClassDao_Impl
我已经尝试了一个多小时来解决这个问题,但没有成功。
编辑:
这是我的 Dao
:
@Dao
public interface MyDemoClassDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(MyDemoClass<AnotherDemoClass> myDemoClass);
}
如果您的回答有任何帮助,请与我分享!
I'd like to use MyDemoClass as a playlist. I'd add Songs to this playlist and save it inside the database
MyDemoClass
将是一个普通的 ol' Java object (POJO),并且具有与播放列表的简单属性相对应的字段,例如 String
这将作为用户可以提供并在播放列表名册中看到的名称。 MyDemoClass
不会 是 ArrayList
的子类。
Song
将是另一个 POJO,并且具有对应于歌曲的简单属性的字段,例如 String
的标题。
由于一个播放列表可以有多首歌曲,并且一首歌曲可以出现在多个播放列表中,因此您需要创建一个实体来表示该连接,并将外键关系返回到播放列表和歌曲实体。
the Room documentation. It should be covered in any book that spends significant time on Room. For example, here is a preview edition of my chapter on M:N relations in Room (from this book).
中简要介绍了外键关系的使用