GreenDao3+ 对应关系

一对一关系 1:1 @ToOne

@Entity
public class Order {
    @Id private Long id;

    private long customerId;

    @ToOne(joinProperty = "customerId")
    private Customer customer;
}

@Entity
public class Customer {
    @Id private Long id;
}

一对多 1:N @ToMany

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

referencedJoinProperty 应该只能是id,如果采用其他字段作为外键就不能用这个了。
joinProperties参数:对于更复杂的关系,您可以指定@JoinProperty注释列表。每个@JoinProperty都需要原始实体中的source属性和目标实体中的引用属性。

@Entity
public class Customer {
    @Id private Long id;
    @Unique private String tag;
 
    @ToMany(joinProperties = {
            @JoinProperty(name = "tag", referencedName = "customerTag")
    })
    @OrderBy("date ASC")
    private List orders;
}
 
@Entity
public class Order {
    @Id private Long id;
    private Date date;
    @NotNull private String customerTag;
}

多对多 n:m @ToMany
使用@JoinEntity 注解 +中间表的形式

@Entity
public class Product {

    @Id
    private Long  id;
    private String name;

    @ToMany
    @JoinEntity(entity =JoinProductsWithOrders.class,sourceProperty = "productId",targetProperty = "orderId")
    private List  orders;
}
@Entity
public class Order {

    @Id
    private  Long id;

    private String  name;

    @ToMany
    @JoinEntity(entity = JoinProductsWithOrders.class,sourceProperty = "orderId",targetProperty = "productId")
    private List  products;
}
@Entity
public class JoinProductsWithOrders {

    @Id
    private Long id;
    private  Long   productId;
    private  Long   orderId;
}

build 后即可,不会生成setProduts setOrders。
同时双方不直接持有对方的字段。通过中间表的形式。

  Product  product =new Product();
        product.setId(1l);
        product.setName("衣服");
        EntityManager.getInstance().getProductDao().insert(product);

        Product  product1 =new Product();
        product1.setId(2l);
        product1.setName("裤子");
        EntityManager.getInstance().getProductDao().insert(product1);

        Order  order =new Order();
        order.setId(1l);
        order.setName("订单1");
        EntityManager.getInstance().getOrderDao().insert(order);

        Order  order1 =new Order();
        order1.setId(2l);
        order1.setName("订单2");
        EntityManager.getInstance().getOrderDao().insert(order1);

       //订单1包含商品1,商品2 
        JoinProductsWithOrders  jw =new JoinProductsWithOrders();
        jw.setOrderId(1l);
        jw.setProductId(1l);
        EntityManager.getInstance().getJoinProductsWithOrders().insert(jw);


        JoinProductsWithOrders  jws =new JoinProductsWithOrders();
        jws.setOrderId(1l);
        jws.setProductId(2l);
        EntityManager.getInstance().getJoinProductsWithOrders().insert(jws);

        //订单2 包含 商品1,商品2
        JoinProductsWithOrders jw1 =new JoinProductsWithOrders();
        jw1.setOrderId(2l);
        jw1.setProductId(1l);
        EntityManager.getInstance().getJoinProductsWithOrders().insert(jw1);

        JoinProductsWithOrders jw1s =new JoinProductsWithOrders();
        jw1s.setOrderId(2l);
        jw1s.setProductId(2l);
        EntityManager.getInstance().getJoinProductsWithOrders().insert(jw1s);
       //经过上面中间表的操作后,商品1,商品2分别再订单1,订单2中了
 List  orders=  EntityManager.getInstance().getOrderDao().queryBuilder().list();
              for (Order  order:orders){
                  for (Product product:order.getProducts()){
                      Log.e("返回",product.getName()+"~~~"+product.getId());
                  }
              }

输出:

08-07 16:48:34.944 13460-13460/com.green.dao E/返回: 订单1
08-07 16:48:34.946 13460-13460/com.green.dao E/返回: 衣服~~~1
08-07 16:48:34.946 13460-13460/com.green.dao E/返回: 裤子~~~2
08-07 16:48:34.950 13460-13460/com.green.dao E/返回: 订单2
08-07 16:48:34.951 13460-13460/com.green.dao E/返回: 衣服~~~1
08-07 16:48:34.951 13460-13460/com.green.dao E/返回: 裤子~~~2

官网:http://greenrobot.org/greendao/documentation/relations/

你可能感兴趣的:(GreenDao3+ 对应关系)