命名实体图子子图 [英] Named Entity Graph Sub-Subgraph

查看:111
本文介绍了命名实体图子子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to JPA 2.1 and started using only recently Named Entity Graphs. For my project I am mapping the following relation in JPA 2.1:

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

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

I want to instruct JPA to join and fetch properly all the needed data. So far this works flawlessly for Order -> OrderDetail -> Product but I have not managed so far to add a Sub-Sub Graph in order to go as deep as the ProductLine class. How do I make a subgraph of a subgraph ? Ex get the ProductLine of the Product ?

这是我的实体(省略了getter和setter):

Here are my entities (getters and setters omitted):

订单

@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;
}

OrderDetail

@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;

ProductLine

@Entity
@Table(name = "productlines")
public class ProductLine {
    @Id
    @Column(name = "productLine")
    private String line;

    @Column
    private String textDescription;


推荐答案

简单的答案是你不能这样做因为,使用当前的JPA实现,您最终会做两个单独的查询并且必须处理笛卡尔积。 JPA的某些未来版本可以扩展到包含更多级别的子图,但是现在它没有。有一个JPA SPEC组可以在JPA的下一个版本上运行。 随时提交您的请求/建议那里

The simple answer is that you cannot do this because, with the current JPA implementation, you would end up doing two separate queries and having to deal with the Cartesian Products. Some future version of JPA could be extended to include more levels of subgraphs, but as it stands today it does not. There is a JPA SPEC group that works on the next version of JPA. Feel free to submit your request/suggestion there.

在StockOverflow上有对同一问题的另一个引用

Here on StockOverflow there is another reference to the same question.

这篇关于命名实体图子子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆