Skip to content

Commit 2daccac

Browse files
committed
HHH-19532 Add unwrap() method to StatelessSession/SharedSessionContract
1 parent 1e028f4 commit 2daccac

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
@@ -1494,4 +1494,9 @@ public interface Session extends SharedSessionContract, EntityManager {
14941494
*/
14951495
@Override @Deprecated(since = "6.0") @SuppressWarnings("rawtypes")
14961496
Query createQuery(CriteriaUpdate updateQuery);
1497+
1498+
@Override
1499+
default <T> T unwrap(Class<T> type) {
1500+
return SharedSessionContract.super.unwrap(type);
1501+
}
14971502
}

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
@@ -2948,7 +2948,8 @@ public <T> T unwrap(Class<T> type) {
29482948
return type.cast( persistenceContext );
29492949
}
29502950

2951-
throw new PersistenceException( "Hibernate cannot unwrap EntityManager as '" + type.getName() + "'" );
2951+
throw new PersistenceException(
2952+
"Hibernate cannot unwrap '" + getClass().getName() + "' as '" + type.getName() + "'" );
29522953
}
29532954

29542955
@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;
@@ -1427,6 +1428,18 @@ public Object loadFromSecondLevelCache(EntityPersister persister, EntityKey enti
14271428
return CacheLoadHelper.loadFromSecondLevelCache( this, instanceToLoad, lockMode, persister, entityKey );
14281429
}
14291430

1431+
@Override
1432+
public <T> T unwrap(Class<T> type) {
1433+
checkOpen();
1434+
1435+
if ( type.isInstance( this ) ) {
1436+
return type.cast( this );
1437+
}
1438+
1439+
throw new PersistenceException(
1440+
"Hibernate cannot unwrap '" + getClass().getName() + "' as '" + type.getName() + "'" );
1441+
}
1442+
14301443
private static final class MultiLoadOptions implements MultiIdLoadOptions {
14311444
private final LockOptions lockOptions;
14321445

0 commit comments

Comments
 (0)