How to use bi-directional Many To Many relationship with GreenDAO?

300 Views Asked by At

After Reading the doc's Relations pages, I can use a many to many relation like this:

@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;
}

Now, with this code, can I have a bi-directionel relations and access from an Order the list of Products associated with it? Or should I add a Product List in the Order class too? Something like this:

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

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

There are 1 best solutions below

0
On

This is how you should do it:

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

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