Relationships using the same model classes for Ormlite & Gson

I have 2 api endpoints that return songs and artists. In my android app, I hit these endpoints with Retrofit, which uses GsonConverter to return me a list of songs or artist objects based on the endpoint. The objects are then saved using Ormlite.

Their json looks like this

SONGS

[
    {
        id: 1,
        name: "foo", 
        year: 2000, 
        artist_id: 1
    },
    {
        id: 2,
        name: "bar", 
        year: 2002, 
        artist_id: 1
    },
    {
        id: 3,
        name: "foobar", 
        year: 2005, 
        artist_id: 2
    }
]

      

PAINTERS

[
    {
        id: 1,
        name: "artist1", 
        country: "usa"
    },
    {
        id: 2,
        name: "artist2", 
        country: "spain"
    }
]

      

The Java classes are as follows

@DatabaseTable(tableName = "songs")
public class Song {

    @Expose
    @DatabaseField(id = true, canBeNull = false)
    private Integer id; 

    @Expose
    @DatabaseField(canBeNull = false)
    private String name; 

    @Expose
    @DatabaseField
    private Integer year; 

    @Expose
    @DatabaseField(canBeNull = false)
    private Integer artist_id; 
}


@DatabaseTable(tableName = "artists")
public class Artist {

    @Expose
    @DatabaseField(id = true, canBeNull = false)
    private Integer id; 

    @Expose
    @DatabaseField(canBeNull = false)
    private String name; 

    @Expose
    @DatabaseField
    private String country; 
}

      

I know that in Ormlite, in order to have a relationship, the fields must be marked as "foreign" and also the type of this field must be the same as one of the classes stored in the database. Since in my case I am only returning "artist_id" in my json songs and not the whole json object for that artist, how can I implement the relationship?

I believe that something very obvious is missing here. It would be great if someone could point me in the right direction

+3


source to share





All Articles