2016-10-24 29 views
3

閱讀文檔的Relations頁面後,我可以用一個多對多的關係是這樣的:如何使用GreenDAO與雙向多對多關係?

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

    @ToMany 
    @JoinEntity(
      entity = JoinProductsWithOrders.class, 
      sourceProperty = "productId", 
      targetProperty = "orderId" 
    ) 
    private List<Order> ordersWithThisProduct; 
} 

@Entity 
public class JoinProductsWithOrders { 
    @Id private Long id; 
    private Long productId; 
    private Long orderId; 
} 

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

現在,有了這個代碼,我可以有一個雙向directionel關係,在獲得從訂購產品的列表與之相關? 或者我應該在Order類中添加產品列表嗎?事情是這樣的:

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

    @ToMany //I don't know if this is corect btw. 
    private List<Product> productsForThisOrder; 
} 

回答

0

這是你應該怎麼做:

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

    @ToMany 
    @JoinEntity(
     entity = JoinProductsWithOrders.class, 
     sourceProperty = "orderId", 
     targetProperty = "productId" 
    ) 
    private List<Product> productsForThisOrder; 
} 
+0

這不是雙directionel多對多的關係。 – HabibKazemi

相關問題