Skip to content

Commit 292f775

Browse files
committed
HHH-19532 Add unwrap() method to StatelessSession/SharedSessionContract
1 parent 651b0af commit 292f775

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,4 +1499,9 @@ public interface Session extends SharedSessionContract, EntityManager {
14991499
*/
15001500
@Override @Deprecated(since = "6.0") @SuppressWarnings("rawtypes")
15011501
Query createQuery(CriteriaUpdate updateQuery);
1502+
1503+
@Override
1504+
default <T> T unwrap(Class<T> type) {
1505+
return SharedSessionContract.super.unwrap(type);
1506+
}
15021507
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.function.Function;
1111

1212
import jakarta.persistence.EntityGraph;
13+
import jakarta.persistence.PersistenceException;
1314
import org.hibernate.graph.RootGraph;
1415
import org.hibernate.jdbc.ReturningWork;
1516
import org.hibernate.jdbc.Work;
@@ -472,4 +473,28 @@ default <R> R fromTransaction(Function<? super Transaction,R> action) {
472473
final Transaction transaction = beginTransaction();
473474
return manageTransaction( transaction, transaction, action );
474475
}
476+
477+
/**
478+
* Return an object of the specified type to allow access to
479+
* a provider-specific API.
480+
*
481+
* @param type the class of the object to be returned.
482+
* This is usually either the underlying class
483+
* implementing {@code SharedSessionContract} or an
484+
* interface it implements.
485+
* @return an instance of the specified class
486+
* @throws PersistenceException if the provider does not
487+
* support the given type
488+
*/
489+
default <T> T unwrap(Class<T> type) {
490+
// Not checking type.isInstance(...) because some implementations
491+
// might want to hide that they implement some types.
492+
// Implementations wanting a more liberal behavior need to override this method.
493+
if ( type.isAssignableFrom( SharedSessionContract.class ) ) {
494+
return type.cast( this );
495+
}
496+
497+
throw new PersistenceException(
498+
"Hibernate cannot unwrap '" + getClass().getName() + "' as '" + type.getName() + "'" );
499+
}
475500
}

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2963,7 +2963,8 @@ public <T> T unwrap(Class<T> type) {
29632963
return type.cast( persistenceContext );
29642964
}
29652965

2966-
throw new PersistenceException( "Hibernate cannot unwrap EntityManager as '" + type.getName() + "'" );
2966+
throw new PersistenceException(
2967+
"Hibernate cannot unwrap '" + getClass().getName() + "' as '" + type.getName() + "'" );
29672968
}
29682969

29692970
@Override

hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Set;
1010
import java.util.function.BiConsumer;
1111

12+
import jakarta.persistence.PersistenceException;
1213
import org.hibernate.AssertionFailure;
1314
import org.hibernate.FlushMode;
1415
import org.hibernate.HibernateException;
@@ -1433,6 +1434,18 @@ public Object loadFromSecondLevelCache(EntityPersister persister, EntityKey enti
14331434
return CacheLoadHelper.loadFromSecondLevelCache( this, instanceToLoad, lockMode, persister, entityKey );
14341435
}
14351436

1437+
@Override
1438+
public <T> T unwrap(Class<T> type) {
1439+
checkOpen();
1440+
1441+
if ( type.isInstance( this ) ) {
1442+
return type.cast( this );
1443+
}
1444+
1445+
throw new PersistenceException(
1446+
"Hibernate cannot unwrap '" + getClass().getName() + "' as '" + type.getName() + "'" );
1447+
}
1448+
14361449
private static final class MultiLoadOptions implements MultiIdLoadOptions {
14371450
private final LockOptions lockOptions;
14381451

0 commit comments

Comments
 (0)