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?
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.
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.
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 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' .
You could use the overloaded method:
Subgraph<Order> egChild = eg.addSubgraph(Customer_.orders.getName(), Order.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With