Skip to content

Commit 51c9d54

Browse files
committed
more work on type inference/safety
this time graphs and domain metamodel
1 parent 1c548c0 commit 51c9d54

28 files changed

+178
-182
lines changed

hibernate-core/src/main/java/org/hibernate/SessionFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ default <T> RootGraph<T> parseEntityGraph(Class<T> rootEntityClass, CharSequence
543543
*
544544
* @since 7.0
545545
*/
546+
@Incubating
546547
default <T> RootGraph<T> parseEntityGraph(String rootEntityName, CharSequence graphText) {
547548
return GraphParser.parse( rootEntityName, graphText.toString(), unwrap( SessionFactoryImplementor.class ) );
548549
}
@@ -560,6 +561,7 @@ default <T> RootGraph<T> parseEntityGraph(String rootEntityName, CharSequence gr
560561
*
561562
* @since 7.0
562563
*/
564+
@Incubating
563565
default <T> RootGraph<T> parseEntityGraph(CharSequence graphText) {
564566
return GraphParser.parse( graphText.toString(), unwrap( SessionFactoryImplementor.class ) );
565567
}

hibernate-core/src/main/java/org/hibernate/boot/model/NamedGraphCreator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
@FunctionalInterface
1616
public interface NamedGraphCreator {
17-
<T> RootGraphImplementor<T> createEntityGraph(
18-
Function<Class<T>, EntityDomainType<?>> entityDomainClassResolver,
17+
RootGraphImplementor<?> createEntityGraph(
18+
Function<Class<?>, EntityDomainType<?>> entityDomainClassResolver,
1919
Function<String, EntityDomainType<?>> entityDomainNameResolver);
2020
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/NamedGraphCreatorJpa.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import jakarta.persistence.NamedAttributeNode;
88
import jakarta.persistence.NamedEntityGraph;
9+
import org.checkerframework.checker.nullness.qual.NonNull;
910
import org.hibernate.AnnotationException;
1011
import org.hibernate.boot.model.NamedGraphCreator;
1112
import org.hibernate.graph.internal.RootGraphImpl;
@@ -36,16 +37,16 @@ class NamedGraphCreatorJpa implements NamedGraphCreator {
3637
}
3738

3839
@Override
39-
public <T> RootGraphImplementor<T> createEntityGraph(
40-
Function<Class<T>, EntityDomainType<?>> entityDomainClassResolver,
40+
public RootGraphImplementor<?> createEntityGraph(
41+
Function<Class<?>, EntityDomainType<?>> entityDomainClassResolver,
4142
Function<String, EntityDomainType<?>> entityDomainNameResolver) {
42-
//noinspection unchecked
43-
final var rootEntityType =
44-
(EntityDomainType<T>)
45-
entityDomainNameResolver.apply( jpaEntityName );
43+
return createGraph( (EntityDomainType<?>)
44+
entityDomainNameResolver.apply( jpaEntityName ) );
45+
}
46+
47+
private <T> @NonNull RootGraphImplementor<T> createGraph(EntityDomainType<T> rootEntityType) {
4648
final var entityGraph =
4749
createRootGraph( name, rootEntityType, annotation.includeAllAttributes() );
48-
4950
final var subclassSubgraphs = annotation.subclassSubgraphs();
5051
if ( subclassSubgraphs != null ) {
5152
for ( var subclassSubgraph : subclassSubgraphs ) {
@@ -59,11 +60,9 @@ public <T> RootGraphImplementor<T> createEntityGraph(
5960
entityGraph.addTreatedSubgraph( subgraphType.asSubclass( graphJavaType ) ) );
6061
}
6162
}
62-
6363
if ( annotation.attributeNodes() != null ) {
6464
applyNamedAttributeNodes( annotation.attributeNodes(), annotation, entityGraph );
6565
}
66-
6766
return entityGraph;
6867
}
6968

hibernate-core/src/main/java/org/hibernate/boot/model/internal/NamedGraphCreatorParsed.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,17 @@ class NamedGraphCreatorParsed implements NamedGraphCreator {
4141
}
4242

4343
@Override
44-
public <T> RootGraphImplementor<T> createEntityGraph(
45-
Function<Class<T>, EntityDomainType<?>> entityDomainClassResolver,
44+
public RootGraphImplementor<?> createEntityGraph(
45+
Function<Class<?>, EntityDomainType<?>> entityDomainClassResolver,
4646
Function<String, EntityDomainType<?>> entityDomainNameResolver) {
47-
final GraphLanguageLexer lexer = new GraphLanguageLexer( CharStreams.fromString( annotation.graph() ) );
48-
final GraphLanguageParser parser = new GraphLanguageParser( new CommonTokenStream( lexer ) );
49-
final GraphLanguageParser.GraphContext graphContext = parser.graph();
47+
final var lexer = new GraphLanguageLexer( CharStreams.fromString( annotation.graph() ) );
48+
final var parser = new GraphLanguageParser( new CommonTokenStream( lexer ) );
49+
final var graphContext = parser.graph();
5050

51-
final EntityNameResolver entityNameResolver = new EntityNameResolver() {
51+
final var entityNameResolver = new EntityNameResolver() {
5252
@Override
53-
public <T> EntityDomainType<T> resolveEntityName(String entityName) {
54-
//noinspection unchecked
55-
final EntityDomainType<T> entityDomainType = (EntityDomainType<T>) entityDomainNameResolver.apply( entityName );
53+
public EntityDomainType<?> resolveEntityName(String entityName) {
54+
final var entityDomainType = (EntityDomainType<?>) entityDomainNameResolver.apply( entityName );
5655
if ( entityDomainType != null ) {
5756
return entityDomainType;
5857
}
@@ -65,17 +64,15 @@ public <T> EntityDomainType<T> resolveEntityName(String entityName) {
6564
throw new InvalidGraphException( "Expecting graph text to include an entity name : " + annotation.graph() );
6665
}
6766
final String jpaEntityName = graphContext.typeIndicator().TYPE_NAME().toString();
68-
//noinspection unchecked
69-
final EntityDomainType<T> entityDomainType = (EntityDomainType<T>) entityDomainNameResolver.apply( jpaEntityName );
67+
final var entityDomainType = entityDomainNameResolver.apply( jpaEntityName );
7068
final String name = this.name == null ? jpaEntityName : this.name;
7169
return GraphParsing.parse( name, entityDomainType, graphContext.attributeList(), entityNameResolver );
7270
}
7371
else {
7472
if ( graphContext.typeIndicator() != null ) {
7573
throw new InvalidGraphException( "Expecting graph text to not include an entity name : " + annotation.graph() );
7674
}
77-
//noinspection unchecked
78-
final EntityDomainType<T> entityDomainType = (EntityDomainType<T>) entityDomainClassResolver.apply( (Class<T>) entityType );
75+
final var entityDomainType = entityDomainClassResolver.apply( entityType );
7976
final String name = this.name == null ? entityDomainType.getName() : this.name;
8077
return GraphParsing.parse( name, entityDomainType, graphContext.attributeList(), entityNameResolver );
8178
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/NaturalIdBinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static Identifier uniqueKeyName(MetadataBuildingContext context, Annotat
5353

5454
private static void addColumnsToUniqueKey(AnnotatedColumns columns, Identifier name) {
5555
final var collector = columns.getBuildingContext().getMetadataCollector();
56-
final Table table = columns.getTable();
56+
final var table = columns.getTable();
5757
final var uniqueKey = table.getOrCreateUniqueKey( name.render( collector.getDatabase().getDialect() ) );
5858
final var property = columns.resolveProperty();
5959
if ( property.isComposite() ) {

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import jakarta.persistence.EntityManagerFactory;
1010
import jakarta.persistence.Subgraph;
1111

12+
import org.hibernate.Incubating;
1213
import org.hibernate.SessionFactory;
1314
import org.hibernate.engine.spi.SessionFactoryImplementor;
1415
import org.hibernate.engine.spi.SessionImplementor;
@@ -89,14 +90,16 @@ public static <T> RootGraph<T> parse(
8990
*
9091
* @since 7.0
9192
*/
93+
@Incubating
9294
public static <T> RootGraph<T> parse(
9395
final String rootEntityName,
9496
final CharSequence graphText,
9597
final SessionFactory sessionFactory) {
9698
if ( graphText == null ) {
9799
return null;
98100
}
99-
return GraphParsing.parse(
101+
//noinspection unchecked
102+
return (RootGraph<T>) GraphParsing.parse(
100103
rootEntityName,
101104
graphText.toString(),
102105
sessionFactory.unwrap( SessionFactoryImplementor.class )
@@ -117,16 +120,18 @@ public static <T> RootGraph<T> parse(
117120
*
118121
* @since 7.0
119122
*/
120-
public static <T> RootGraph<T> parse(
123+
@Incubating
124+
public static <T> RootGraph<T> parse(
121125
final CharSequence graphText,
122126
final SessionFactory sessionFactory) {
123127
if ( graphText == null ) {
124128
return null;
125129
}
126-
return GraphParsing.parse(
130+
//noinspection unchecked
131+
return (RootGraph<T>) GraphParsing.parse(
127132
graphText.toString(),
128133
sessionFactory.unwrap( SessionFactoryImplementor.class )
129-
);
134+
);
130135
}
131136

132137
/**
@@ -153,7 +158,8 @@ public static <T> RootGraph<T> parse(
153158
return GraphParsing.parse(
154159
rootType,
155160
graphText.toString(),
156-
entityManager.getEntityManagerFactory().unwrap( SessionFactoryImplementor.class )
161+
entityManager.getEntityManagerFactory()
162+
.unwrap( SessionFactoryImplementor.class )
157163
);
158164
}
159165

@@ -190,13 +196,12 @@ public static <T> void parseInto(
190196
*
191197
* @throws InvalidGraphException if the textual representation is invalid.
192198
*/
193-
@SuppressWarnings("unchecked")
194199
public static <T> void parseInto(
195-
final EntityGraph<T> graph,
200+
final EntityGraph<?> graph,
196201
final CharSequence graphText,
197202
final EntityManager entityManager) {
198203
parseInto(
199-
(GraphImplementor<T>) graph,
204+
(GraphImplementor<?>) graph,
200205
graphText,
201206
( (SessionImplementor) entityManager ).getSessionFactory()
202207
);
@@ -211,13 +216,12 @@ public static <T> void parseInto(
211216
*
212217
* @throws InvalidGraphException if the textual representation is invalid.
213218
*/
214-
@SuppressWarnings("unchecked")
215-
public static <T> void parseInto(
216-
final Subgraph<T> graph,
219+
public static void parseInto(
220+
final Subgraph<?> graph,
217221
final CharSequence graphText,
218222
final EntityManager entityManager) {
219223
parseInto(
220-
(GraphImplementor<T>) graph,
224+
(GraphImplementor<?>) graph,
221225
graphText,
222226
( (SessionImplementor) entityManager ).getSessionFactory()
223227
);
@@ -232,12 +236,12 @@ public static <T> void parseInto(
232236
*
233237
* @throws InvalidGraphException if the textual representation is invalid.
234238
*/
235-
public static <T> void parseInto(
236-
final Graph<T> graph,
239+
public static void parseInto(
240+
final Graph<?> graph,
237241
final CharSequence graphText,
238242
final EntityManagerFactory entityManagerFactory) {
239243
parseInto(
240-
(GraphImplementor<T>) graph,
244+
(GraphImplementor<?>) graph,
241245
graphText,
242246
(SessionFactoryImplementor) entityManagerFactory
243247
);
@@ -252,13 +256,12 @@ public static <T> void parseInto(
252256
*
253257
* @throws InvalidGraphException if the textual representation is invalid.
254258
*/
255-
@SuppressWarnings("unchecked")
256-
public static <T> void parseInto(
257-
final EntityGraph<T> graph,
259+
public static void parseInto(
260+
final EntityGraph<?> graph,
258261
final CharSequence graphText,
259262
final EntityManagerFactory entityManagerFactory) {
260263
parseInto(
261-
(GraphImplementor<T>) graph,
264+
(GraphImplementor<?>) graph,
262265
graphText,
263266
(SessionFactoryImplementor) entityManagerFactory
264267
);
@@ -273,13 +276,12 @@ public static <T> void parseInto(
273276
*
274277
* @throws InvalidGraphException if the textual representation is invalid.
275278
*/
276-
@SuppressWarnings("unchecked")
277-
public static <T> void parseInto(
278-
final Subgraph<T> graph,
279+
public static void parseInto(
280+
final Subgraph<?> graph,
279281
final CharSequence graphText,
280282
final EntityManagerFactory entityManagerFactory) {
281283
parseInto(
282-
(GraphImplementor<T>) graph,
284+
(GraphImplementor<?>) graph,
283285
graphText,
284286
(SessionFactoryImplementor) entityManagerFactory
285287
);
@@ -298,8 +300,8 @@ public static <T> void parseInto(
298300
*
299301
* @throws InvalidGraphException if the textual representation is invalid.
300302
*/
301-
private static <T> void parseInto(
302-
GraphImplementor<T> graph,
303+
private static void parseInto(
304+
GraphImplementor<?> graph,
303305
final CharSequence graphText,
304306
SessionFactoryImplementor sessionFactory) {
305307
if ( graphText != null ) {

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,13 @@ public SubGraphImplementor<E> makeSubGraph() {
258258

259259
@Override @Deprecated
260260
public <S> SubGraphImplementor<S> makeSubGraph(Class<S> subtype) {
261-
final ManagedDomainType<E> managedType = asManagedType( valueGraphType );
261+
final var managedType = asManagedType( valueGraphType );
262262
if ( !managedType.getJavaType().isAssignableFrom( subtype ) ) {
263263
throw new IllegalArgumentException( "Not a subtype: " + subtype.getName() );
264264
}
265265
@SuppressWarnings("unchecked")
266-
final Class<? extends E> castSuptype = (Class<? extends E>) subtype;
267-
final SubGraphImplementor<? extends E> result = makeSubGraph().addTreatedSubgraph( castSuptype );
266+
final var castSuptype = (Class<? extends E>) subtype;
267+
final var result = makeSubGraph().addTreatedSubgraph( castSuptype );
268268
//noinspection unchecked
269269
return (SubGraphImplementor<S>) result;
270270
}
@@ -282,13 +282,13 @@ public SubGraphImplementor<K> makeKeySubGraph() {
282282
@Override @Deprecated
283283
public <S> SubGraphImplementor<S> makeKeySubGraph(Class<S> subtype) {
284284
checkMap();
285-
final ManagedDomainType<K> type = asManagedType( keyGraphType );
285+
final var type = asManagedType( keyGraphType );
286286
if ( !type.getJavaType().isAssignableFrom( subtype ) ) {
287287
throw new IllegalArgumentException( "Not a key subtype: " + subtype.getName() );
288288
}
289289
@SuppressWarnings("unchecked")
290-
final Class<? extends K> castType = (Class<? extends K>) subtype;
291-
final SubGraphImplementor<? extends K> result = makeKeySubGraph().addTreatedSubgraph( castType );
290+
final var castType = (Class<? extends K>) subtype;
291+
final var result = makeKeySubGraph().addTreatedSubgraph( castType );
292292
//noinspection unchecked
293293
return (SubGraphImplementor<S>) result;
294294
}
@@ -323,7 +323,7 @@ public String toString() {
323323
public void merge(AttributeNodeImplementor<J, E, K> that) {
324324
assert that.isMutable() == isMutable();
325325
assert that.getAttributeDescriptor() == attribute;
326-
final SubGraphImplementor<E> otherValueSubgraph = that.getValueSubgraph();
326+
final var otherValueSubgraph = that.getValueSubgraph();
327327
if ( otherValueSubgraph != null ) {
328328
if ( valueSubgraph == null ) {
329329
valueSubgraph = otherValueSubgraph.makeCopy( isMutable() );
@@ -333,7 +333,7 @@ public void merge(AttributeNodeImplementor<J, E, K> that) {
333333
valueSubgraph.mergeInternal( otherValueSubgraph );
334334
}
335335
}
336-
final SubGraphImplementor<K> otherKeySubgraph = that.getKeySubgraph();
336+
final var otherKeySubgraph = that.getKeySubgraph();
337337
if ( otherKeySubgraph != null ) {
338338
if ( keySubgraph == null ) {
339339
keySubgraph = otherKeySubgraph.makeCopy( isMutable() );
@@ -351,7 +351,8 @@ public Map<Class<?>, SubGraphImplementor<?>> getSubGraphs() {
351351
return emptyMap();
352352
}
353353
else {
354-
final HashMap<Class<?>, SubGraphImplementor<?>> map = new HashMap<>( valueSubgraph.getTreatedSubgraphs() );
354+
final HashMap<Class<?>, SubGraphImplementor<?>> map =
355+
new HashMap<>( valueSubgraph.getTreatedSubgraphs() );
355356
map.put( attribute.getValueGraphType().getJavaType(), valueSubgraph );
356357
return map;
357358
}
@@ -363,7 +364,8 @@ public Map<Class<?>, SubGraphImplementor<?>> getKeySubGraphs() {
363364
return emptyMap();
364365
}
365366
else {
366-
final HashMap<Class<?>, SubGraphImplementor<?>> map = new HashMap<>( keySubgraph.getTreatedSubgraphs() );
367+
final HashMap<Class<?>, SubGraphImplementor<?>> map =
368+
new HashMap<>( keySubgraph.getTreatedSubgraphs() );
367369
map.put( attribute.getKeyGraphType().getJavaType(), keySubgraph );
368370
return map;
369371
}

0 commit comments

Comments
 (0)