Skip to content

Commit 3086df6

Browse files
committed
HHH-19612 allow EntityGraph names in the JPA standard hints
and fix a mistake in the Javadoc fot SharedSessionContract
1 parent f6af272 commit 3086df6

File tree

6 files changed

+51
-25
lines changed

6 files changed

+51
-25
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,23 +375,26 @@ public interface SharedSessionContract extends QueryProducer, AutoCloseable, Ser
375375
* @throws IllegalArgumentException if the graph with the given
376376
* name does not have the given entity type as its root
377377
*
378+
* @see jakarta.persistence.EntityManager#createEntityGraph(String)
379+
*
378380
* @since 6.3
379381
*/
380382
<T> RootGraph<T> createEntityGraph(Class<T> rootType, String graphName);
381383

382384
/**
383385
* Obtain an immutable reference to a predefined
384-
* {@linkplain jakarta.persistence.NamedEntityGraph named entity graph}
385-
* or return {@code null} if there is no predefined graph with the given
386-
* name.
386+
* {@linkplain jakarta.persistence.NamedEntityGraph named entity graph}.
387387
*
388388
* @param graphName the name of the predefined named entity graph
389+
* @throws IllegalArgumentException if there is no predefined graph
390+
* with the given name
389391
*
390392
* @apiNote This method returns {@code RootGraph<?>}, requiring an
391393
* unchecked typecast before use. It's cleaner to obtain a graph using
392394
* the static metamodel for the class which defines the graph, or by
393395
* calling {@link SessionFactory#getNamedEntityGraphs(Class)} instead.
394396
*
397+
* @see jakarta.persistence.EntityManager#getEntityGraph(String)
395398
* @see SessionFactory#getNamedEntityGraphs(Class)
396399
* @see jakarta.persistence.EntityManagerFactory#addNamedEntityGraph(String, EntityGraph)
397400
*

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionImplementor.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.hibernate.Session;
1111
import org.hibernate.engine.jdbc.LobCreationContext;
1212
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
13-
import org.hibernate.graph.spi.RootGraphImplementor;
1413
import org.hibernate.persister.entity.EntityPersister;
1514
import org.hibernate.query.spi.QueryImplementor;
1615
import org.hibernate.resource.jdbc.spi.JdbcSessionOwner;
@@ -66,15 +65,6 @@ default SessionImplementor getSession() {
6665
@Override
6766
SessionFactoryImplementor getSessionFactory();
6867

69-
@Override
70-
<T> RootGraphImplementor<T> createEntityGraph(Class<T> rootType);
71-
72-
@Override
73-
RootGraphImplementor<?> createEntityGraph(String graphName);
74-
75-
@Override
76-
RootGraphImplementor<?> getEntityGraph(String graphName);
77-
7868
@Override
7969
<T> QueryImplementor<T> createQuery(CriteriaSelect<T> selectQuery);
8070

hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.hibernate.bytecode.enhance.spi.interceptor.SessionAssociationMarkers;
2121
import org.hibernate.dialect.Dialect;
2222
import org.hibernate.event.spi.EventSource;
23+
import org.hibernate.graph.spi.RootGraphImplementor;
2324
import org.hibernate.query.Query;
2425
import org.hibernate.SharedSessionContract;
2526
import org.hibernate.Transaction;
@@ -584,4 +585,13 @@ default boolean isStatelessSession() {
584585
*/
585586
@Incubating
586587
SessionAssociationMarkers getSessionAssociationMarkers();
588+
589+
@Override
590+
<T> RootGraphImplementor<T> createEntityGraph(Class<T> rootType);
591+
592+
@Override
593+
RootGraphImplementor<?> createEntityGraph(String graphName);
594+
595+
@Override
596+
RootGraphImplementor<?> getEntityGraph(String graphName);
587597
}

hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionDelegatorBaseImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.hibernate.engine.jdbc.spi.JdbcServices;
3030
import org.hibernate.event.monitor.spi.EventMonitor;
3131
import org.hibernate.graph.RootGraph;
32+
import org.hibernate.graph.spi.RootGraphImplementor;
3233
import org.hibernate.jdbc.ReturningWork;
3334
import org.hibernate.jdbc.Work;
3435
import org.hibernate.persister.entity.EntityPersister;
@@ -632,12 +633,12 @@ public TimeZone getJdbcTimeZone() {
632633
}
633634

634635
@Override
635-
public <T> RootGraph<T> createEntityGraph(Class<T> rootType) {
636+
public <T> RootGraphImplementor<T> createEntityGraph(Class<T> rootType) {
636637
return delegate.createEntityGraph( rootType );
637638
}
638639

639640
@Override
640-
public RootGraph<?> createEntityGraph(String graphName) {
641+
public RootGraphImplementor<?> createEntityGraph(String graphName) {
641642
return delegate.createEntityGraph( graphName );
642643
}
643644

@@ -647,7 +648,7 @@ public <T> RootGraph<T> createEntityGraph(Class<T> rootType, String graphName) {
647648
}
648649

649650
@Override
650-
public RootGraph<?> getEntityGraph(String graphName) {
651+
public RootGraphImplementor<?> getEntityGraph(String graphName) {
651652
return delegate.getEntityGraph( graphName );
652653
}
653654

hibernate-core/src/main/java/org/hibernate/query/spi/AbstractCommonQueryContract.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,13 @@ protected final boolean applySelectionHint(String hintName, Object value) {
354354
DEPRECATION_LOGGER.deprecatedSetting( HINT_JAVAEE_FETCH_GRAPH, HINT_SPEC_FETCH_GRAPH );
355355
//fall through to:
356356
case HINT_SPEC_FETCH_GRAPH:
357-
applyEntityGraphHint( hintName, value );
357+
applyEntityGraphHint( GraphSemantic.FETCH, value, hintName );
358358
return true;
359359
case HINT_JAVAEE_LOAD_GRAPH:
360360
DEPRECATION_LOGGER.deprecatedSetting( HINT_JAVAEE_LOAD_GRAPH, HINT_SPEC_LOAD_GRAPH );
361361
//fall through to:
362362
case HINT_SPEC_LOAD_GRAPH:
363-
applyEntityGraphHint( hintName, value );
363+
applyEntityGraphHint( GraphSemantic.LOAD, value, hintName );
364364
return true;
365365
case HINT_FETCH_PROFILE:
366366
queryOptions.enableFetchProfile( (String) value );
@@ -371,17 +371,39 @@ protected final boolean applySelectionHint(String hintName, Object value) {
371371
}
372372
}
373373

374-
protected void applyEntityGraphHint(String hintName, Object value) {
375-
final GraphSemantic graphSemantic = GraphSemantic.fromHintName( hintName );
374+
protected void applyEntityGraphHint(GraphSemantic graphSemantic, Object value, String hintName) {
376375
if ( value instanceof RootGraphImplementor<?> rootGraphImplementor ) {
377376
applyGraph( rootGraphImplementor, graphSemantic );
378377
}
379378
else if ( value instanceof String string ) {
380-
applyGraph( string, graphSemantic );
379+
// try and interpret it as the name of a @NamedEntityGraph
380+
final var entityGraph = getEntityGraph( string );
381+
if ( entityGraph == null ) {
382+
try {
383+
// try and parse it in the entity graph language
384+
applyGraph( string, graphSemantic );
385+
}
386+
catch ( IllegalArgumentException e ) {
387+
throw new IllegalArgumentException( "The string value of the hint '" + hintName
388+
+ "' must be the name of a named EntityGraph, or a representation understood by GraphParser" );
389+
}
390+
}
391+
else {
392+
applyGraph( entityGraph, graphSemantic );
393+
}
381394
}
382395
else {
383396
throw new IllegalArgumentException( "The value of the hint '" + hintName
384-
+ "' must be an instance of EntityGraph or the string name of a named EntityGraph" );
397+
+ "' must be an instance of EntityGraph, the string name of a named EntityGraph, or a string representation understood by GraphParser" );
398+
}
399+
}
400+
401+
private RootGraphImplementor<?> getEntityGraph(String string) {
402+
try {
403+
return getSession().getEntityGraph( string );
404+
}
405+
catch ( IllegalArgumentException e ) {
406+
return null;
385407
}
386408
}
387409

@@ -392,7 +414,7 @@ protected void applyGraph(String graphString, GraphSemantic graphSemantic) {
392414
throw new IllegalArgumentException(
393415
String.format(
394416
ROOT,
395-
"Invalid entity-graph definition `%s`; expected form `${EntityName}( ${property1} ... )",
417+
"Invalid entity-graph definition '%s'; expected form '${EntityName}( ${property1} ... )'",
396418
graphString
397419
)
398420
);

hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,8 @@ public Query<R> applyGraph(@SuppressWarnings("rawtypes") RootGraph graph, GraphS
673673
}
674674

675675
@Override
676-
protected void applyEntityGraphHint(String hintName, Object value) {
677-
super.applyEntityGraphHint( hintName, value );
676+
protected void applyEntityGraphHint(GraphSemantic graphSemantic, Object value, String hintName) {
677+
super.applyEntityGraphHint( graphSemantic, value, hintName );
678678
}
679679

680680
@Override

0 commit comments

Comments
 (0)