Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using Entity Graphs in JPA 2.1, is there a way of using the metamodel when there are Subgraphs of Subgraphs?

For example, consider a Customer entity has a Set of Orders. Each Order has a Set of OrderItems.

I can do this with named attributes:

EntityGraph<Customer> eg = em.createEntityGraph(Customer.class);
Subgraph<Order> egChild = eg.addSubgraph("orders");
egChild.addAttributeNodes("orderItems");

If I was only interested in Orders, I can do this using the metamodel:

EntityGraph<Customer> eg = em.createEntityGraph(Customer.class);
eg.addSubgraph(Customer_.orders);

But, If I want the entire graph using only the metamodel, I can not do this:

EntityGraph<Customer> eg = em.createEntityGraph(Customer.class);
Subgraph<Set<Order>> egChild = eg.addSubgraph(Customer_.orders);
egChild.addAttributeNodes(Order_.orderItems);

The problem seems to be that

eg.addSubgraph(Customer_.orders)

returns a

Subgraph<Set<Order>> 

and not a

Subgraph<Order>

Is this a shortcoming of metamodel/entitygraphs, or am I missing something?

like image 856
Jim Cox Avatar asked Feb 18 '16 14:02

Jim Cox


People also ask

What is entity graph in JPA?

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 the use of NamedEntityGraph?

NamedEntityGraph annotation defines a single named entity graph and is applied at the class level. Multiple @NamedEntityGraph annotations may be defined for a class by adding them within a javax. persistence. NamedEntityGraphs class-level annotation.

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 Subgraph JPA?

What are Entity Subgraphs? In JPA, a sub entity graph is used to define "fetch plan" for an entity involving relationship to this entity. Subgraphs are defined by using @NamedSubgraph annotation. This annotation is used within the main @NamedEntityGraph annotation (last tutorial) via the attribute of 'subgraphs' .


1 Answers

You could use the overloaded method:

Subgraph<Order> egChild = eg.addSubgraph(Customer_.orders.getName(), Order.class);
like image 197
Marinos An Avatar answered Sep 20 '22 01:09

Marinos An