命名实体图子子图

我是JPA 2.1的新手,并且最近才开始使用命名实体图。 对于我的项目,我在JPA 2.1中映射了以下关系:

订单 – >订单详细信息 – >产品 – > ProductLine

问题是:

我想指示JPA加入并正确获取所有需要的数据。 到目前为止,这对于Order – > OrderDetail – > Product来说是完美无缺的,但到目前为止我还没有管理过添加Sub-Sub Graph以便像ProductLine类一样深入。 如何制作子图的子图? Ex获取产品的ProductLine?

这是我的实体(省略了getters和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 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上有另一个对同一问题的引用 。