0% found this document useful (0 votes)
65 views

EntityGraphs With Hibernate 54

Hibernate 5.4 introduces two improvements to entity graphs: 1) An entity graph can now be parsed from a string, allowing the fetching behavior to be defined programmatically. 2) Multiple entity graphs can be merged into a single graph, allowing graphs to be defined independently and then combined.

Uploaded by

Adolf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

EntityGraphs With Hibernate 54

Hibernate 5.4 introduces two improvements to entity graphs: 1) An entity graph can now be parsed from a string, allowing the fetching behavior to be defined programmatically. 2) Multiple entity graphs can be merged into a single graph, allowing graphs to be defined independently and then combined.

Uploaded by

Adolf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EntityGraphs with Hibernate 5.

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.

Parse a String into an EntityGraph


Hibernate’s GraphParser class provides a set of static methods to
parse a String into a new RootGraph object or to parse it into an
existing RootGraph or SubGraph object.
The format of the String is pretty simple. You start on the root entity
and specify a comma-separated list of attributes, which you want to
include in the graph.

RootGraph graph = GraphParser.parse(Order.class, "items", em);

Order newOrder = em.find(Order.class, 1L,


Collections.singletonMap("javax.persistence.fetchgraph", graph));

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.

RootGraph graph = GraphParser.parse(Order.class, "items(product(supplier))", em);

Order newOrder = em.find(Order.class, 1L,


Collections.singletonMap("javax.persistence.fetchgraph", graph));

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

Order newOrder = em.find(Order.class, 1L,


Collections.singletonMap("javax.persistence.fetchgraph", graph));

www.thoughts-on-java.org

You might also like