3

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;
}
Asme Just
  • 1,287
  • 5
  • 27
  • 42

1 Answers1

0

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;
}
noamt
  • 7,397
  • 2
  • 37
  • 59
whopp3r
  • 86
  • 3