3

阅读文档的关系页面后,我可以使用这样的多对多关系:

@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 访问与其关联的产品列表吗?或者我也应该在 Order 类中添加一个产品列表?像这样的东西:

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

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

1 回答 1

0

你应该这样做:

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

    @ToMany
    @JoinEntity(
        entity = JoinProductsWithOrders.class,
        sourceProperty = "orderId",
        targetProperty = "productId"
    )
    private List<Product>  productsForThisOrder;
}
于 2017-09-17T07:47:49.677 回答