Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named Entity Graph Sub-Subgraph

Tags:

java

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:

Order -> OrderDetail -> Product -> ProductLine

The question:

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 ?

Here are my entities (getters and setters omitted):

Order

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

Product

@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; 
like image 642
sashok_bg Avatar asked Jul 21 '16 13:07

sashok_bg


People also ask

What is the use of @entitygraph?

JPA 2.1 has introduced the Entity Graph feature as a more sophisticated method of dealing with performance loading. It allows defining a template by grouping the related persistence fields which we want to retrieve and lets us choose the graph type at runtime.

What is a named entity graph?

Named entity graphs are created using annotations applied to entity classes or the named-entity-graph element and its sub-elements in the application's deployment descriptor. The persistence provider will scan for all named entity graphs, defined in both annotations and in XML, within an application.

What is entity graph in Entity Framework?

When instantiated objects are joined together in a relationship they are referred to as a graph, or an entity graph. The Entity Framework has some important rules about how graphs are maintained. Example, if you have a User(Entity) graph that consists of a User with Roles, Features.

What is entity graph in spring boot?

1. Overview. Simply put, Entity Graphs are another way to describe a query in JPA 2.1. We can use them to formulate better-performing queries. In this tutorial, we're going to learn how to implement Entity Graphs with Spring Data JPA through a simple example.


1 Answers

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.

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

like image 134
Daniel Wisehart Avatar answered Sep 23 '22 10:09

Daniel Wisehart