2

I have a problem with greenDao and @ToMany relation. Drning session when i'm doing insertion to DB i can get @ToMany relation easily. My problem occurs after I'm restarting my application, list that is being returned is empty.

Daily.class

@Entity
public class Daily extends BaseObservable implements Parcelable {

@Id(autoincrement = true)
private Long id;

@Unique
private String tag;

@SerializedName("summary")
@Expose
private String summary;

@SerializedName("icon")
@Expose
private String icon;

@SerializedName("data")
@Expose
@ToMany(joinProperties = {
        @JoinProperty(name = "tag", referencedName = "dataTag")})
@OrderBy("id ASC")
private List<DailyData> data = new ArrayList<DailyData>();

DailyData.class

@Entity
public class DailyData extends BaseObservable implements Parcelable {

@Id(autoincrement = true)
private Long id;

@NotNull private String dataTag;
... [rest of the code]

Place where I'm doing insert to DB (I'm using rxjava2 to get model). I've been trying inserting it in different order but there is no affect also.

public void insertCityToDatabase(City city) {

    city.setName(cityLatLngList.get(getPosition()).getResult().getName());

    city.getDailyWithoutId().setTag(city.getName());
    city.getHourlyWithoutId().setTag(city.getName());

    for (HourlyData hourlyData : city.getHourlyWithoutId().getHourlyDataWithoutId()) {
        hourlyData.setDataTag(city.getName());
        daoSession.insert(hourlyData);
    }
    for (DailyData dailyData : city.getDailyWithoutId().getDataWithoutId()) {
        dailyData.setDataTag(city.getName());
        daoSession.insert(dailyData);
    }

    daoSession.insert(city.getHourlyWithoutId());
    daoSession.insert(city.getDailyWithoutId());
    daoSession.insert(city.getCurrentlyWithoutId());

    long id = daoSession.insert(city);

    eventBus.post(new NewCity(id));
}

Here is my DB Daily and DailyData, debuger Android Studio

I have also tried doing @ToMany relation by @ToMany(referencedJoinProperty = "id") but the result is the same.

Any idea why @ToMany relation is not working?

DawidJ
  • 1,245
  • 12
  • 19
  • Did you solved it? I have the same problem – user3384194 Jan 08 '17 at 22:28
  • No, i was forced to move from greenDao to Realm cause of this problem. Once i tried to test greenDao at new project and it was working but i have no idea why it wasn't working here. – DawidJ Jan 09 '17 at 11:32
  • It seems that you should have a property Daily in DailyData. In this way is similar to JPA. – jmoran Apr 06 '18 at 02:12

1 Answers1

0

I solve the problem just now. Your Daily.class:

@SerializedName("data")
@Expose
@ToMany(joinProperties = {
    @JoinProperty(name = "tag", referencedName = "dataTag")})
@OrderBy("id ASC")
private List<DailyData> data = new ArrayList<DailyData>();

and DailyData.class:

@Entity
public class DailyData extends BaseObservable implements Parcelable {

@Id(autoincrement = true)
private Long id;

@NotNull private String dataTag;
... [rest of the code]

This means that field tag in Daily.class has relation with field dataTag in DailyData.class. So when you insert to DB:

for (DailyData dailyData : city.getDailyWithoutId().getDataWithoutId()){
    dailyData.setDataTag(city.getTag());//
    daoSession.insert(dailyData);
}

I solve the problem in this way.

Yt100
  • 324
  • 4
  • 12
  • I did it, but it says: "Dataz is not an entity, but it is referenced for @ToMany relation in class (field: data)";;;;; Question: http://stackoverflow.com/questions/44082952/custom-type-in-greendao-with-pojo-class – Dr.jacky May 20 '17 at 09:33