Skip to content

Commit bd885b6

Browse files
committed
Graphpocalypse: major revision/refactoring of EntityGraph support
- add Javadoc, which has been missing for many years - add a missing operation
1 parent e5a94c4 commit bd885b6

File tree

5 files changed

+209
-33
lines changed

5 files changed

+209
-33
lines changed

hibernate-core/src/main/java/org/hibernate/graph/AttributeNode.java

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,50 @@
1414
import static java.util.Collections.unmodifiableMap;
1515

1616
/**
17-
* Extends the JPA-defined {@link AttributeNode} with additional operations.
17+
* Represents a fetched {@linkplain jakarta.persistence.metamodel.Attribute attribute} in an
18+
* {@linkplain Graph entity graph}.
19+
* <p>
20+
* An {@code AttributeNode} representing an attribute whose type is a managed type or collection
21+
* of some managed type may have an associated <em>value subgraph</em>, which is represented by
22+
* an instance of {@link SubGraph}.
23+
* <ul>
24+
* <li>For a {@linkplain jakarta.persistence.metamodel.SingularAttribute singular attribute},
25+
* the value type is the type of the attribute.
26+
* <li>For a {@linkplain jakarta.persistence.metamodel.PluralAttribute plural attribute}, the
27+
* value type is the collection element type.
28+
* </ul>
29+
* <p>
30+
* Or, if the represented attribute is a {@link Map}, the {@code AttributeNode} maye have an
31+
* associated <em>key subgraph</em>, similarly represented by a {@link SubGraph}.
32+
* <p>
33+
* Not every attribute node has a subgraph.
34+
* <p>
35+
* Extends the JPA-defined {@link jakarta.persistence.AttributeNode} with additional operations.
1836
*
1937
* @author Strong Liu
2038
* @author Steve Ebersole
2139
* @author Andrea Boriero
40+
* @author Gavin King
2241
*/
2342
public interface AttributeNode<J> extends GraphNode<J>, jakarta.persistence.AttributeNode<J> {
2443

44+
/**
45+
* The {@link PersistentAttribute} represented by this node.
46+
*/
2547
PersistentAttribute<?, J> getAttributeDescriptor();
2648

49+
/**
50+
* All value subgraphs rooted at this node.
51+
*
52+
* @see jakarta.persistence.AttributeNode#getSubgraphs
53+
*/
2754
Map<Class<?>, ? extends SubGraph<?>> getSubGraphs();
55+
56+
/**
57+
* All key subgraphs rooted at this node.
58+
*
59+
* @see jakarta.persistence.AttributeNode#getKeySubgraphs
60+
*/
2861
Map<Class<?>, ? extends SubGraph<?>> getKeySubGraphs();
2962

3063
@Override
@@ -37,13 +70,43 @@ public interface AttributeNode<J> extends GraphNode<J>, jakarta.persistence.Attr
3770
return unmodifiableMap( getKeySubGraphs() );
3871
}
3972

73+
/**
74+
* Create and return a new value {@link SubGraph} rooted at this node,
75+
* or return an existing such {@link SubGraph} if there is one.
76+
*/
4077
SubGraph<?> makeSubGraph();
78+
79+
/**
80+
* Create and return a new key {@link SubGraph} rooted at this node,
81+
* or return an existing such {@link SubGraph} if there is one.
82+
*/
4183
SubGraph<?> makeKeySubGraph();
4284

85+
/**
86+
* Create and return a new value {@link SubGraph} rooted at this node,
87+
* with the given type, which may be a subtype of the value type,
88+
* or return an existing such {@link SubGraph} if there is one.
89+
*/
4390
<S> SubGraph<S> makeSubGraph(Class<S> subtype);
91+
92+
/**
93+
* Create and return a new value {@link SubGraph} rooted at this node,
94+
* with the given type, which may be a subtype of the key type,
95+
* or return an existing such {@link SubGraph} if there is one.
96+
*/
4497
<S> SubGraph<S> makeKeySubGraph(Class<S> subtype);
4598

99+
/**
100+
* Create and return a new value {@link SubGraph} rooted at this node,
101+
* with the given type, which may be a subtype of the value type,
102+
* or return an existing such {@link SubGraph} if there is one.
103+
*/
46104
<S> SubGraph<S> makeSubGraph(ManagedDomainType<S> subtype);
47-
<S> SubGraph<S> makeKeySubGraph(ManagedDomainType<S> subtype);
48105

106+
/**
107+
* Create and return a new value {@link SubGraph} rooted at this node,
108+
* with the given type, which may be a subtype of the key type,
109+
* or return an existing such {@link SubGraph} if there is one.
110+
*/
111+
<S> SubGraph<S> makeKeySubGraph(ManagedDomainType<S> subtype);
49112
}

hibernate-core/src/main/java/org/hibernate/graph/Graph.java

Lines changed: 127 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,36 @@
77
import java.util.List;
88

99
import jakarta.persistence.metamodel.Attribute;
10+
import jakarta.persistence.metamodel.MapAttribute;
1011
import jakarta.persistence.metamodel.PluralAttribute;
1112
import org.hibernate.metamodel.model.domain.ManagedDomainType;
1213
import org.hibernate.metamodel.model.domain.MapPersistentAttribute;
1314
import org.hibernate.metamodel.model.domain.PersistentAttribute;
1415
import org.hibernate.metamodel.model.domain.PluralPersistentAttribute;
1516

1617
/**
17-
* A container for {@link AttributeNode} references.
18-
*
19-
* @apiNote Acts as an abstraction over the JPA-defined interfaces
20-
* {@link jakarta.persistence.EntityGraph} and
21-
* {@link jakarta.persistence.Subgraph}, which have no
22-
* common supertype.
18+
* Represents a {@link jakarta.persistence.metamodel.ManagedType managed type} in an
19+
* {@linkplain Graph entity graph}, acting as a container for:
20+
* <ul>
21+
* <li>{@link AttributeNode} references representing fetched attributes, and
22+
* <li><em>treated subgraphs</em>, each represented by a child instance of
23+
* {@link SubGraph}.
24+
* </ul>
25+
* <p>
26+
* A treated (narrowed) subgraph allows fetching to be specified for any attribute of
27+
* any subtype of the type represented by this graph. The usual way to create a treated
28+
* subgraph is by calling {@link jakarta.persistence.EntityGraph#addTreatedSubgraph(Class)}
29+
* or {@link #addTreatedSubGraph(Class)}. There are various shortcut operations such as
30+
* {@link jakarta.persistence.EntityGraph#addTreatedSubgraph(Attribute, Class)} and
31+
* {@link #addSubGraph(PersistentAttribute, Class)} which combine creation of a subgraph
32+
* with creation of a treated subgraph.
33+
* <p>
34+
* Extends the JPA-defined {@link jakarta.persistence.Graph} with additional operations.
2335
*
2436
* @author Strong Liu
2537
* @author Steve Ebersole
2638
* @author Andrea Boriero
39+
* @author Gavin King
2740
*
2841
* @see RootGraph
2942
* @see SubGraph
@@ -33,23 +46,17 @@
3346
public interface Graph<J> extends GraphNode<J>, jakarta.persistence.Graph<J> {
3447

3548
/**
36-
* Get a list of all existing AttributeNodes within this container.
37-
*
38-
* @see #getAttributeNodes
39-
*/
40-
List<? extends AttributeNode<?>> getAttributeNodeList();
41-
42-
/**
43-
* Graphs apply only to {@link jakarta.persistence.metamodel.ManagedType}s.
49+
* The {@linkplain jakarta.persistence.metamodel.ManagedType managed type}
50+
* of the node.
4451
*
4552
* @return the {@code ManagedType} being graphed here.
4653
*/
4754
ManagedDomainType<J> getGraphedType();
4855

4956
/**
50-
* Create a named root {@link Graph} if the given name is not null.
57+
* Create a named {@linkplain RootGraph root graph} representing this node.
5158
*
52-
* @param mutable controls whether the resulting {@code Graph} is mutable
59+
* @param mutable controls whether the resulting graph is mutable
5360
*
5461
* @throws CannotBecomeEntityGraphException If the named attribute is not entity-valued
5562
*
@@ -60,20 +67,32 @@ RootGraph<J> makeRootGraph(String name, boolean mutable)
6067
throws CannotBecomeEntityGraphException;
6168

6269
/**
63-
* Create a new (mutable or immutable) {@link SubGraph} rooted at
64-
* this {@link Graph}.
70+
* Create a new {@linkplain SubGraph subgraph} representing this node.
6571
*
6672
* @deprecated This will be removed
6773
*/
6874
@Deprecated(since = "7.0", forRemoval = true)
6975
SubGraph<J> makeSubGraph(boolean mutable);
7076

77+
/**
78+
* Make a copy of this graph node, with the given mutability.
79+
* <p>
80+
* If this graph is immutable, and the argument is {@code false},
81+
* simply return this instance.
82+
*/
7183
@Override
7284
Graph<J> makeCopy(boolean mutable);
7385

7486

7587
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76-
// AttributeNode handling
88+
// AttributeNodes
89+
90+
/**
91+
* All {@linkplain AttributeNode nodes} belonging to this container.
92+
*
93+
* @see #getAttributeNodes
94+
*/
95+
List<? extends AttributeNode<?>> getAttributeNodeList();
7796

7897
/**
7998
* Find an already existing AttributeNode by attributeName within
@@ -112,8 +131,8 @@ default boolean hasAttributeNode(Attribute<? super J, ?> attribute) {
112131
}
113132

114133
/**
115-
* Add an {@link AttributeNode} (with no associated {@link SubGraph})
116-
* to this container by attribute reference.
134+
* Add an {@link AttributeNode} representing the given {@link PersistentAttribute}
135+
* to this node of the graph without creating any associated {@link SubGraph}.
117136
*
118137
* @see #addAttributeNode(Attribute)
119138
*/
@@ -126,53 +145,135 @@ default <Y> AttributeNode<Y> addAttributeNode(Attribute<? super J, Y> attribute)
126145

127146

128147
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129-
// Subgraph nodes
148+
// Subgraphs
130149

150+
/**
151+
* Create and return a new (mutable) {@link SubGraph} representing
152+
* the given subtype of the type of this node, or return an existing
153+
* such {@link SubGraph} if there is one.
154+
*
155+
* @see jakarta.persistence.EntityGraph#addTreatedSubgraph(Class)
156+
*/
131157
<Y extends J> SubGraph<Y> addTreatedSubGraph(Class<Y> type);
132158

159+
/**
160+
* Create and return a new (mutable) {@link SubGraph} representing
161+
* the given subtype of the type of this node, or return an existing
162+
* such {@link SubGraph} if there is one.
163+
*/
133164
<Y extends J> SubGraph<Y> addTreatedSubGraph(ManagedDomainType<Y> type);
134165

135166
/**
136167
* Create and return a new (mutable) {@link SubGraph} associated with
137-
* the named {@link AttributeNode}.
168+
* the named {@link Attribute}, or return an existing such {@link SubGraph}
169+
* if there is one.
138170
*
139-
* @apiNote If no such AttributeNode exists yet, it is created.
171+
* @see #addSubgraph(String)
140172
*/
141173
@Deprecated
142174
<AJ> SubGraph<AJ> addSubGraph(String attributeName)
143175
throws CannotContainSubGraphException;
144176

177+
/**
178+
* Create and return a new (mutable) {@link SubGraph} associated with
179+
* the named {@link Attribute}, and with the given type, which may be
180+
* a subtype of the attribute type, or return an existing such
181+
* {@link SubGraph} if there is one.
182+
*
183+
* @see #addSubgraph(String, Class)
184+
*/
145185
<AJ> SubGraph<AJ> addSubGraph(String attributeName, Class<AJ> type)
146186
throws CannotContainSubGraphException;
147187

148188
/**
149189
* Create and return a new (mutable) {@link SubGraph} associated with
150-
* the {@link AttributeNode} for the given attribute.
190+
* the given {@link PersistentAttribute}, or return an existing such
191+
* {@link SubGraph} if there is one.
151192
*
152-
* @apiNote If no such AttributeNode exists yet, it is created.
193+
* @see #addSubgraph(Attribute)
153194
*/
154195
<AJ> SubGraph<AJ> addSubGraph(PersistentAttribute<? super J, AJ> attribute)
155196
throws CannotContainSubGraphException;
156197

198+
/**
199+
* Create and return a new (mutable) {@link SubGraph} associated with
200+
* the given {@link PersistentAttribute}, and with the given type,
201+
* which may be a subtype of the attribute type, or return an existing
202+
* such {@link SubGraph} if there is one.
203+
*
204+
* @see #addSubgraph(Attribute, Class)
205+
*/
157206
<AJ> SubGraph<AJ> addSubGraph(PersistentAttribute<? super J, ? super AJ> attribute, Class<AJ> type)
158207
throws CannotContainSubGraphException;
159208

209+
/**
210+
* Create and return a new (mutable) {@link SubGraph} associated with
211+
* the given {@link PersistentAttribute}, and with the given type,
212+
* which may be a subtype of the attribute type, or return an existing
213+
* such {@link SubGraph} if there is one.
214+
*/
160215
<AJ> SubGraph<AJ> addSubGraph(PersistentAttribute<? super J, ? super AJ> attribute, ManagedDomainType<AJ> type)
161216
throws CannotContainSubGraphException;
162217

218+
/**
219+
* Create and return a new (mutable) {@link SubGraph} associated with
220+
* the element of the given collection, and with the given type, which
221+
* may be a subtype of the attribute type, or return an existing such
222+
* {@link SubGraph} if there is one.
223+
*
224+
* @see #addTreatedElementSubgraph(PluralAttribute, Class)
225+
*/
163226
<AJ> SubGraph<AJ> addElementSubGraph(PluralPersistentAttribute<? super J, ?, ? super AJ> attribute, Class<AJ> type)
164227
throws CannotContainSubGraphException;
165228

229+
/**
230+
* Create and return a new (mutable) {@link SubGraph} associated with
231+
* the element of the given collection, and with the given type, which
232+
* may be a subtype of the attribute type, or return an existing such
233+
* {@link SubGraph} if there is one.
234+
*/
166235
<AJ> SubGraph<AJ> addElementSubGraph(PluralPersistentAttribute<? super J, ?, ? super AJ> attribute, ManagedDomainType<AJ> type)
167236
throws CannotContainSubGraphException;
168237

238+
/**
239+
* Create and return a new (mutable) {@link SubGraph} associated with
240+
* the key of the named map or return an existing such {@link SubGraph}
241+
* if there is one.
242+
*
243+
* @see #addKeySubgraph(String)
244+
*/
169245
@Deprecated
170246
<AJ> SubGraph<AJ> addKeySubGraph(String attributeName)
171247
throws CannotContainSubGraphException;
172248

249+
/**
250+
* Create and return a new (mutable) {@link SubGraph} associated with
251+
* the key of the named map, and with the given type, which may be a
252+
* subtype of the attribute type, or return an existing such
253+
* {@link SubGraph} if there is one.
254+
*
255+
* @see #addKeySubgraph(String, Class)
256+
*/
173257
<AJ> SubGraph<AJ> addKeySubGraph(String attributeName, Class<AJ> type)
174258
throws CannotContainSubGraphException;
175259

260+
/**
261+
* Create and return a new (mutable) {@link SubGraph} associated with
262+
* the key of the named map, and with the given type, which may be a
263+
* subtype of the attribute type, or return an existing such
264+
* {@link SubGraph} if there is one.
265+
*
266+
* @see #addTreatedMapKeySubgraph(MapAttribute, Class)
267+
*/
268+
<AJ> SubGraph<AJ> addKeySubGraph(MapPersistentAttribute<? super J, ? super AJ, ?> attribute, Class<AJ> type)
269+
throws CannotContainSubGraphException;
270+
271+
/**
272+
* Create and return a new (mutable) {@link SubGraph} associated with
273+
* the key of the named map, and with the given type, which may be a
274+
* subtype of the attribute type, or return an existing such
275+
* {@link SubGraph} if there is one.
276+
*/
176277
<AJ> SubGraph<AJ> addKeySubGraph(MapPersistentAttribute<? super J, ? super AJ, ?> attribute, ManagedDomainType<AJ> type)
177278
throws CannotContainSubGraphException;
178279

hibernate-core/src/main/java/org/hibernate/graph/internal/AbstractGraph.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* Base class for {@link RootGraph} and {@link SubGraph} implementations.
3939
*
4040
* @author Steve Ebersole
41+
* @author Gavin King
4142
*/
4243
public abstract class AbstractGraph<J> extends AbstractGraphNode<J> implements GraphImplementor<J> {
4344

@@ -319,6 +320,11 @@ public <AJ> SubGraphImplementor<AJ> addElementSubGraph(PluralPersistentAttribute
319320
return findOrCreateAttributeNode( attribute ).makeSubGraph( type );
320321
}
321322

323+
@Override
324+
public <AJ> SubGraphImplementor<AJ> addKeySubGraph(MapPersistentAttribute<? super J, ? super AJ, ?> attribute, Class<AJ> subtype) {
325+
return findOrCreateAttributeNode( attribute ).makeKeySubGraph( subtype );
326+
}
327+
322328
@Override
323329
public <AJ> SubGraphImplementor<AJ> addKeySubGraph(MapPersistentAttribute<? super J, ? super AJ, ?> attribute, ManagedDomainType<AJ> subtype) {
324330
return findOrCreateAttributeNode( attribute ).makeKeySubGraph( subtype );

hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Implementation of {@link jakarta.persistence.AttributeNode}.
2323
*
2424
* @author Steve Ebersole
25+
* @author Gavin King
2526
*/
2627
public class AttributeNodeImpl<J,V,K>
2728
extends AbstractGraphNode<J>

0 commit comments

Comments
 (0)