EntityGraphs With Hibernate 54
EntityGraphs With Hibernate 54
4
Hibernate 5.4 brings two improvements to this approach. Hibernate
can now parse an Entity Graph from a provided String, and it enables
you to merge multiple graphs into one.
For a lot of use cases, you also might want to define the fetching
behavior of an associated entity. You can do that by providing a
comma-separated list of attribute names in round brackets.
When you execute this short code snippet, you will see that
Hibernate uses this EntityGraph in the same way as it uses a graph
defined via the @NamedEntityGraph annotation or the Entity Graph
API. It extends the SQL statement to select all database columns that
are mapped by the entities included in the graph.
www.thoughts-on-java.org
EntityGraphs with Hibernate 5.4
Combine multiple graphs into one EntityGraph
Another feature added in Hibernate 5.4 enables you to merge
multiple graphs into one. That allows you to define graphs indepently
of each other and combine them if needed.
The only thing you need to do to merge multiple graphs is to call the
static merge method on Hibernate’s EntityGraphs class with
references to the current Session or EntityManager, the class on
which the graph shall be applied and multiple EntityGraph objects.
RootGraph graph1 = GraphParser.parse(Order.class, "items(product(supplier))", em);
RootGraph graph2 = GraphParser.parse(Order.class, "customer", em);
EntityGraph graph = EntityGraphs.merge(em, Order.class, graph1, graph2);
www.thoughts-on-java.org