5

I want to cache a song-list in my app, the Song-list structure is like below:

 @Entity
 public class Songlist {
      String _id;
      String desc;
      List<SongDesc> songWithComment;
      .....

    public static class SongDesc {
      String comment;
      Song song;
    }
}
 @Entity
 pulbic class Song {
      String name;
      String type;
      ......
 }

The lib of operating sqlite3 is android.arch.persistence.room, but it dosen't allow object references in a table.Is there any way to cache a song-list by using Room in Android?

jaime
  • 51
  • 1
  • 3
  • 2
    You would need separate `@Entity` classes that model the database structure (e.g., `SonglistEntity`, `SongEntity`), where the song has a `@ForeignKey` relationship back to the parent. – CommonsWare Dec 14 '17 at 14:24
  • Thanks for reply! a song may include by countless songlist while a songlist contains limited songs, maybe I should use a `@ForeignKey` in `Songlist`? But I don't know how to deal with the list... – jaime Dec 14 '17 at 14:35

2 Answers2

3

Also if you want to store some custom objects, you can use @Embedded annotation like in example bellow :

class Address {
public String street;
public String state;
public String city;

@ColumnInfo(name = "post_code")
public int postCode; 
}


@Entity
class User {
@PrimaryKey
public int id;

public String firstName;

@Embedded
public Address address;
} 
C. Alen
  • 184
  • 1
  • 10
1

If you want to save an ArrayList of some type into the database, you shuld use TypeConverter to convert the ArrayList from and into a simpler recognizable type to the database engine, like a String.

See this: Android Room Database: How to handle Arraylist in an Entity? and https://commonsware.com/AndroidArch/previews/room-and-custom-types

joao86
  • 2,056
  • 1
  • 21
  • 23