How to set a one-to-many relationship in greenDAO?

226 Views Asked by At

hope you have a good day.

I am new to greenDAO, and am wondering how to model a one-to-many relationship using greenDAO in an Android app so that I can retrieve a list of TestResult entities that belong to a User entity. I've used the @ToMany annotation at the User entity class and @ToOne at TestResult, following this documentation. However, even after persisting TestResult entity objects with the User property set, retrieving the same User causes a null error for its list of TestResult entity objects.

I would be greatly appreciate it if you guys would be so gracious as to to provide some help. Thank you.

1

There are 1 best solutions below

3
Haseeb Mirza On
@Entity
public class Customer {
    @Id private Long id;
 
    @ToMany(referencedJoinProperty = "customerId")
    @OrderBy("date ASC")
    private List<Order> orders;
}
 
@Entity
public class Order {
    @Id private Long id;
    private Date date;
    private long customerId;
}

Customer and Order are two entities. One customer have multiple orders. For this customerId is in order entity.

private long customerId;

In customer Entity,

 @ToMany(referencedJoinProperty = "customerId")
  @OrderBy("date ASC")
  private List<Order> orders;

Reference for details