Skip to content

Commit d443935

Browse files
committed
HHH-19941 deprecate Session.contains(String, Object)
and introduce SharedSessionContractImplementor.isManaged()
1 parent 40e1dfd commit d443935

File tree

8 files changed

+69
-66
lines changed

8 files changed

+69
-66
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,10 @@ public interface Session extends SharedSessionContract, EntityManager {
470470
* @param object an instance of a persistent class
471471
*
472472
* @return {@code true} if the given instance is associated with this {@code Session}
473+
*
474+
* @deprecated Use {@link #contains(Object)} instead.
473475
*/
476+
@Deprecated(since = "7.2", forRemoval = true)
474477
boolean contains(String entityName, Object object);
475478

476479
/**

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@ public boolean contains(Object object) {
800800
return delegate.contains( object );
801801
}
802802

803+
@Override
804+
public boolean isManaged(Object entity) {
805+
return delegate.isManaged( entity );
806+
}
807+
803808
@Override
804809
public LockModeType getLockMode(Object entity) {
805810
return delegate.getLockMode( entity );

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,15 @@ default Integer getConfiguredJdbcBatchSize() {
503503
*/
504504
PersistenceContext getPersistenceContextInternal();
505505

506+
/**
507+
* Is the given entity managed by this session?
508+
*
509+
* @return true if this is a stateful session and
510+
* the entity belongs to its persistence
511+
* context and was not removed
512+
*/
513+
boolean isManaged(Object entity);
514+
506515
/**
507516
* detect in-memory changes, determine if the changes are to tables
508517
* named in the query and, if so, complete execution the flush

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,4 +742,9 @@ public SharedSessionBuilder sessionWithOptions() {
742742
public TransactionCompletionCallbacksImplementor getTransactionCompletionCallbacksImplementor() {
743743
return delegate.getTransactionCompletionCallbacksImplementor();
744744
}
745+
746+
@Override
747+
public boolean isManaged(Object entity) {
748+
return delegate.isManaged( entity );
749+
}
745750
}

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private static void handleUninitializedProxy(
7373
EventSource source,
7474
Object object,
7575
PersistenceContext persistenceContext) {
76-
final boolean isTransient = isTransient( event, source, object );
76+
final boolean isTransient = !source.isManaged( object );
7777
// If refreshAlready is nonempty then the refresh is the result of a cascade refresh and the
7878
// refresh of the parent will take care of initializing the lazy entity and setting the
7979
// correct lock. This is needed only when the refresh is called directly on a lazy entity.
@@ -116,11 +116,6 @@ else if ( isTransient ) {
116116
}
117117
}
118118

119-
private static boolean isTransient(RefreshEvent event, EventSource source, Object object) {
120-
final String entityName = event.getEntityName();
121-
return entityName == null ? !source.contains( object ) : !source.contains( entityName, object );
122-
}
123-
124119
private static void refresh(RefreshEvent event, RefreshContext refreshedAlready, Object object) {
125120
final var source = event.getSession();
126121
final var persistenceContext = source.getPersistenceContextInternal();

hibernate-core/src/main/java/org/hibernate/id/IdentifierGeneratorHelper.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.hibernate.TransientObjectException;
2121
import org.hibernate.boot.registry.selector.spi.StrategySelector;
2222
import org.hibernate.engine.config.spi.ConfigurationService;
23-
import org.hibernate.engine.spi.SessionImplementor;
2423
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2524
import org.hibernate.id.enhanced.ImplicitDatabaseObjectNamingStrategy;
2625
import org.hibernate.id.enhanced.StandardNamingStrategy;
@@ -82,16 +81,15 @@ else if ( integralType == BigDecimal.class ) {
8281

8382
public static Object getForeignId(
8483
String entityName, String propertyName, SharedSessionContractImplementor sessionImplementor, Object object) {
85-
final var persister =
86-
sessionImplementor.getFactory().getMappingMetamodel()
87-
.getEntityDescriptor( entityName );
88-
if ( sessionImplementor instanceof SessionImplementor statefulSession
89-
&& statefulSession.contains( entityName, object ) ) {
84+
if ( sessionImplementor.isManaged( object ) ) {
9085
//abort the save (the object is already saved by a circular cascade)
9186
return SHORT_CIRCUIT_INDICATOR;
9287
//throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association");
9388
}
9489
else {
90+
final var persister =
91+
sessionImplementor.getFactory().getMappingMetamodel()
92+
.getEntityDescriptor( entityName );
9593
return identifier( sessionImplementor, entityType( propertyName, persister ),
9694
associatedEntity( entityName, propertyName, object, persister ) );
9795
}

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

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,13 +1338,45 @@ private void fireRefresh(final RefreshContext refreshedAlready, final RefreshEve
13381338
}
13391339

13401340
private void checkEntityManaged(String entityName, Object entity) {
1341-
if ( !managed( entityName, entity ) ) {
1341+
if ( !isManaged( entity ) ) {
13421342
throw new IllegalArgumentException( "Given entity is not associated with the persistence context" );
13431343
}
13441344
}
13451345

1346-
private boolean managed(String entityName, Object entity) {
1347-
return entityName == null ? contains( entity ) : contains( entityName, entity );
1346+
@Override
1347+
public boolean isManaged(Object entity) {
1348+
try {
1349+
final var lazyInitializer = extractLazyInitializer( entity );
1350+
if ( lazyInitializer != null ) {
1351+
//do not use proxiesByKey, since not all
1352+
//proxies that point to this session's
1353+
//instances are in that collection!
1354+
if ( lazyInitializer.isUninitialized() ) {
1355+
//if it is an uninitialized proxy, pointing
1356+
//with this session, then when it is accessed,
1357+
//the underlying instance will be "contained"
1358+
return lazyInitializer.getSession() == this;
1359+
}
1360+
else {
1361+
//if it is initialized, see if the underlying
1362+
//instance is contained, since we need to
1363+
//account for the fact that it might have been
1364+
//evicted
1365+
entity = lazyInitializer.getImplementation();
1366+
}
1367+
}
1368+
// A session is considered to contain an entity only if the entity has
1369+
// an entry in the session's persistence context and the entry reports
1370+
// that the entity has not been removed
1371+
final var entry = persistenceContext.getEntry( entity );
1372+
return entry != null && !entry.getStatus().isDeletedOrGone();
1373+
}
1374+
catch ( MappingException e ) {
1375+
throw new IllegalArgumentException( e.getMessage(), e );
1376+
}
1377+
catch ( RuntimeException e ) {
1378+
throw getExceptionConverter().convert( e );
1379+
}
13481380
}
13491381

13501382
// replicate() operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1700,58 +1732,9 @@ private void assertInstanceOfEntityType(Object object) {
17001732
}
17011733
}
17021734

1703-
@Override
1735+
@Override @Deprecated(forRemoval = true)
17041736
public boolean contains(String entityName, Object object) {
1705-
checkOpenOrWaitingForAutoClose();
1706-
pulseTransactionCoordinator();
1707-
delayedAfterCompletion();
1708-
1709-
if ( object == null ) {
1710-
return false;
1711-
}
1712-
1713-
try {
1714-
final var lazyInitializer = extractLazyInitializer( object );
1715-
if ( lazyInitializer == null && persistenceContext.getEntry( object ) == null ) {
1716-
// check if it is an entity -> if not throw an exception (per JPA)
1717-
try {
1718-
requireEntityPersister( entityName );
1719-
}
1720-
catch (HibernateException e) {
1721-
throw new IllegalArgumentException( "Not an entity [" + entityName + "] : " + object );
1722-
}
1723-
}
1724-
1725-
if ( lazyInitializer != null ) {
1726-
//do not use proxiesByKey, since not all
1727-
//proxies that point to this session's
1728-
//instances are in that collection!
1729-
if ( lazyInitializer.isUninitialized() ) {
1730-
//if it is an uninitialized proxy, pointing
1731-
//with this session, then when it is accessed,
1732-
//the underlying instance will be "contained"
1733-
return lazyInitializer.getSession() == this;
1734-
}
1735-
else {
1736-
//if it is initialized, see if the underlying
1737-
//instance is contained, since we need to
1738-
//account for the fact that it might have been
1739-
//evicted
1740-
object = lazyInitializer.getImplementation();
1741-
}
1742-
}
1743-
// A session is considered to contain an entity only if the entity has
1744-
// an entry in the session's persistence context and the entry reports
1745-
// that the entity has not been removed
1746-
final var entry = persistenceContext.getEntry( object );
1747-
return entry != null && !entry.getStatus().isDeletedOrGone();
1748-
}
1749-
catch ( MappingException e ) {
1750-
throw new IllegalArgumentException( e.getMessage(), e );
1751-
}
1752-
catch ( RuntimeException e ) {
1753-
throw getExceptionConverter().convert( e );
1754-
}
1737+
return contains( object );
17551738
}
17561739

17571740
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,11 @@ public boolean isIdentifierRollbackEnabled() {
13021302
return false;
13031303
}
13041304

1305+
@Override
1306+
public boolean isManaged(Object entity) {
1307+
return false;
1308+
}
1309+
13051310
/////////////////////////////////////////////////////////////////////////////////////////////////////
13061311

13071312
//TODO: COPY/PASTE FROM SessionImpl, pull up!

0 commit comments

Comments
 (0)