我是JPA 2.1的新手,仅开始使用最近命名的实体图。对于我的项目,我正在JPA 2.1中映射以下关系:
订单-> OrderDetail->产品-> ProductLine
我想指示JPA加入并正确获取所有需要的数据。到目前为止,这对于Order-> OrderDetail-> Product而言是完美无缺的,但是到目前为止,我还没有设法添加Sub-Sub Graph来深入到ProductLine类。如何制作子图的子图?取得产品的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;
简单的答案是您无法执行此操作,因为在当前的JPA实现中,您最终将不得不执行两个单独的查询,并且不得不处理笛卡尔积。JPA的某些将来版本可以扩展为包括更多级别的子图,但就目前而言,还没有。有一个JPA SPEC组可用于下一版JPA。随时在此处提交您的请求/建议。
在StockOverflow上,还有另一个参考相同的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句