I have the following 3 classes:
1) TvShow
@Entity
public class TvShow{
@Id
private String uuid;
private String url;
private String title;
...
@ToMany(referencedJoinProperty = "tvShowUuid")
private List<Episode> episodes;
2) Episode
@Entity
public class Episode{
@Id
private String uuid;
@ToMany(referencedJoinProperty = "episodeUuid")
private List<Moment> moments;
//FK
private String tvShowUuid;
3) Moment
@Entity
public class Moment{
@Id
private String uuid;
...
private String episodeUuid;
As you can see the relationship between them is: TvShow --> oneToMany --> Episode --> oneToMany --> Moment
Assuming I have a fully populated TvShow object (called tvShow), I am doing the following to add that object to my database:
final long rowId = daoSession.getTvShowDao().insertOrReplace(tvShow);
This correctly adds the tvShow object to the TvShow table. However, the Episode and Moment tables are NOT populated at all. The tvShow object has several Episodes and each episode has several moments. I was expecting the other 2 tables to contain that data as well, but they don't.
What am I doing wrong? Am I supposed to loop through each Episode (and in turn loop through each Moment of each episode) and insert them manually instead? =(
Maybe I will be wrong, I have had the same problem, but greenDao is not able to persist cascade data - If you say
insertOrReplace(tvShow)
it will not persist list of episodes and list of moments. In order to perform that you have to persist all data separately.For example:
TvShowDao.save(tvShows)
ORTvShowDao.insertOrUpdate(tvShows);
AND
EpisodesDao.save(episodes)
OREpisodesDao.save(episodes)
AND
MomentDao.save(moments);
ORMomentDao.save(moments);
In other words, you have to persist all data separately.
I hope so that this helped you.