命名实体图子子图

Named Entity Graph Sub-Subgraph

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

订单 -> 订单详情 -> 产品 -> ProductLine

问题:

我想指示 JPA 加入并正确获取所有需要的数据。到目前为止,这对 Order -> OrderDetail -> Product 完美无缺,但到目前为止我还没有设法添加 Sub-Sub Graph 以深入到 ProductLine class.如何制作子图的子图?例如获取产品的 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;

ProductLine

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

    @Column
    private String textDescription;

简单的回答是您不能这样做,因为在当前的 JPA 实现中,您最终会执行两个单独的查询并且必须处理笛卡尔积。 JPA 的某些未来版本可以扩展为包含更多级别的子图,但就目前而言,它并没有。有一个 JPA SPEC 小组致力于下一版本的 JPA。 Feel free to submit your request/suggestion there.

在 StockOverflow 上有

您可以使用动态实体图创建多级实体图。 我正在使用 jpa 2.2 和 Hibernate 5.3.7,我能够创建实体 图表和获取数据最多 3 个级别。我希望这对 下一个级别。下面是代码片段。有关更多详细信息和实际代码,您可以查看我的 github 存储库:https://github.com/vaneetkataria/Jpa-Hibernate/blob/master/jdbcToJpaMigration/src/test/java/com/katariasoft/technologies/jpaHibernate/entity/fetch/entitygraph/dynamic/MultiInstructorsDynamicEntityGrpahTests.java

代码片段:

@SuppressWarnings("unchecked")
    @Test
    @Rollback(false)
    public void fetchInstrctrsIdProofVehiclesStudentsTheirInstructorsVehiclesAndTheirDocuments() {
        doInTransaction(() -> {
            EntityGraph<Instructor> instructorGraph = em.createEntityGraph(Instructor.class);
            instructorGraph.addAttributeNodes(Instructor_.idProof, Instructor_.vehicles);
            Subgraph<Student> studentSubgraph = instructorGraph.addSubgraph(Instructor_.STUDENTS);
            studentSubgraph.addAttributeNodes(Student_.instructors);
            Subgraph<Vehicle> vehicleSubgraph = studentSubgraph.addSubgraph(Student_.VEHICLES);
            vehicleSubgraph.addAttributeNodes(Vehicle_.documents);
            TypedQuery<Instructor> query = em.createQuery("select i from Instructor i ", Instructor.class)
                    .setHint(EntityGraphUtils.FETCH_GRAPH, instructorGraph);
            List<Instructor> instructors = query.getResultList();
            if (Objects.nonNull(instructors))
                instructors.forEach(instructor -> {
                    IdProof idProof = instructor.getIdProof();
                    Set<Vehicle> vehicles = instructor.getVehicles();
                    Set<Student> students = instructor.getStudents();
                    System.out.println(instructor);
                    System.out.println(idProof);
                    if (Objects.nonNull(vehicles))
                        vehicles.forEach(v -> System.out.println(v.getVehicleNumber()));
                    if (Objects.nonNull(students))
                        students.forEach(s -> System.out.println(s.getName()));
                });
        });
    }

每个NamedAttributeNode都可以指定一个子图。

@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(value = "product", subgraph = "graph.Product.productLine")),
        @NamedSubgraph(name = "graph.Product.productLine", attributeNodes = @NamedAttributeNode("line"))
    }
)