简体   繁体   中英

Room: Use class that extends ArrayList as Entity

I am making an Android app using the Room Persistence Library. I have a class that extends the ArrayList class like this:

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

I'm trying to add this class, which only allows classes that extend AnotherDemoClass to be in it, as an entity to Room but there's a compile-time error occurring, which is:

error: cannot find symbol
        _result = new MyDemoClass<E>();
                                  ^
  symbol:   class E
  location: class MyDemoClassDao_Impl

I've been trying for more than an hour to fix this issue without any luck.

EDIT: This is my Dao :

@Dao
public interface MyDemoClassDao {

  @Insert(onConflict = OnConflictStrategy.REPLACE)
  void insert(MyDemoClass<AnotherDemoClass> myDemoClass);

}

If your answer could help in any way, please do share it with me!

I'd like to use MyDemoClass as a playlist. I'd add Songs to this playlist and save it inside the database

MyDemoClass would be a plain ol' Java object (POJO) and would have fields that correspond to simple properties of a playlist, such as a String that would serve as the name that users can provide and see in rosters of playlists. MyDemoClass would not be a subclass of ArrayList .

Song would be another POJO and have fields that correspond to simple properties of a song, such as a String for its title.

Since a playlist can have multiple songs, and a song can appear in multiple playlists, you would need to create an entity that represents that join, with foreign key relationships back to the playlist and song entities.

The use of foreign key relationships is lightly covered in 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM