在阅读了文档的Relations页后,我可以使用这样的多对多关系:
@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;
}
现在,使用此代码,我是否可以拥有双向关系并从订单访问与之关联的产品列表? 或者我应该在Order类中添加产品列表吗?像这样:
...
@Entity
public class Order {
@Id private Long id;
@ToMany //I don't know if this is corect btw.
private List<Product> productsForThisOrder;
}
答案 0 :(得分:0)
这是你应该怎么做的:
@Entity
public class Order {
@Id private Long id;
@ToMany
@JoinEntity(
entity = JoinProductsWithOrders.class,
sourceProperty = "orderId",
targetProperty = "productId"
)
private List<Product> productsForThisOrder;
}