【发布时间】:2016-11-25 03:15:07
【问题描述】:
我是 JPA 2.1 的新手,最近才开始使用命名实体图。对于我的项目,我在 JPA 2.1 中映射以下关系:
订单 -> OrderDetail -> 产品 -> ProductLine
问题:
我想指示 JPA 加入并正确获取所有需要的数据。到目前为止,这对于 Order -> OrderDetail -> Product 来说完美无缺,但到目前为止我还没有设法添加一个 Sub-Sub Graph 以便深入到 ProductLine 类。如何制作子图的子图? Ex 获取产品的 ProductLine ?
这是我的实体(省略了 getter 和 setter):
订购
@Entity
@Table(name="ORDERS")
@NamedEntityGraph(
name = "graph.Order.details",
attributeNodes = {
@NamedAttributeNode(value = "details", subgraph = "graph.OrderDetail.product")
},
subgraphs = {
@NamedSubgraph(name = "graph.OrderDetail.product", attributeNodes = @NamedAttributeNode("product"))
}
)
public class Order implements Serializable{
@Id
@Column(name = "orderNumber")
private Long number;
@Column(name = "orderDate")
private Date date;
@OneToMany(mappedBy = "order")
private List<OrderDetail> details;
}
订单详情
@Entity
@Table(name = "orderdetails")
public class OrderDetail implements Serializable{
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "orderNumber")
@Id
private Order order;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "productCode", nullable = false)
@Id
private Product product;
@Column(name = "orderLineNumber")
private int lineNumber;
@Column(name = "quantityOrdered")
private int quantity;
产品
@Entity
@Table(name = "products")
class Product {
@Column(name = "productCode")
@Id
private String code;
@Column(name = "quantityInStock")
public int quantity;
@ManyToOne
@JoinColumn(name = "productLine")
private ProductLine line;
产品线
@Entity
@Table(name = "productlines")
public class ProductLine {
@Id
@Column(name = "productLine")
private String line;
@Column
private String textDescription;
【问题讨论】:
-
不确定这是否可能,但是如果您在子图下创建另一个 NamedSubgraph,将其类型指定为 Product,attributenodes 为 line,然后从 @NamedAttributeNode("产品”)你有吗?