Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
*/
package org.hibernate;

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

import jakarta.persistence.FindOption;
import jakarta.persistence.metamodel.EntityType;
import org.hibernate.graph.RootGraph;
import org.hibernate.jdbc.Work;
import org.hibernate.query.Query;
Expand Down Expand Up @@ -1267,6 +1269,44 @@ public interface Session extends SharedSessionContract, EntityManager {
*/
LobHelper getLobHelper();

/**
* Obtain the collection of all managed entities which belong to this
* persistence context.
*
* @since 7.0
*/
@Incubating
Collection<?> getManagedEntities();

/**
* Obtain a collection of all managed instances of the entity type with the
* given entity name which belong to this persistence context.
*
* @since 7.0
*/
@Incubating
Collection<?> getManagedEntities(String entityName);

/**
* Obtain a collection of all managed entities of the given type which belong
* to this persistence context. This operation is not polymorphic, and does
* not return instances of subtypes of the given entity type.
*
* @since 7.0
*/
@Incubating
<E> Collection<E> getManagedEntities(Class<E> entityType);

/**
* Obtain a collection of all managed entities of the given type which belong
* to this persistence context. This operation is not polymorphic, and does
* not return instances of subtypes of the given entity type.
*
* @since 7.0
*/
@Incubating
<E> Collection<E> getManagedEntities(EntityType<E> entityType);

/**
* Obtain a {@link Session} builder with the ability to copy certain
* information from this session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@

import org.checkerframework.checker.nullness.qual.Nullable;

import static java.util.Collections.emptyMap;
import static org.hibernate.engine.internal.ManagedTypeHelper.asHibernateProxy;
import static org.hibernate.engine.internal.ManagedTypeHelper.asManagedEntity;
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
import static org.hibernate.internal.util.collections.CollectionHelper.mapOfSize;
import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer;

/**
Expand Down Expand Up @@ -183,7 +185,7 @@

private Map<EntityKey, EntityHolderImpl> getOrInitializeEntitiesByKey() {
if ( entitiesByKey == null ) {
entitiesByKey = CollectionHelper.mapOfSize( INIT_COLL_SIZE );
entitiesByKey = mapOfSize( INIT_COLL_SIZE );
}
return entitiesByKey;
}
Expand Down Expand Up @@ -484,17 +486,10 @@
postLoadEvent.setEntity( holder.getEntity() )
.setId( holder.getEntityKey().getIdentifier() )
.setPersister( holder.getDescriptor() );
listenerGroup.fireEventOnEachListener(
postLoadEvent,
PostLoadEventListener::onPostLoad
);
listenerGroup.fireEventOnEachListener( postLoadEvent, PostLoadEventListener::onPostLoad );

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning

Variable
listenerGroup
may be null at this access because of
this
null argument.
}
if ( callback != null ) {
callback.invokeAfterLoadActions(
holder.getEntity(),
holder.getDescriptor(),
getSession()
);
callback.invokeAfterLoadActions( holder.getEntity(), holder.getDescriptor(), getSession() );
}
holder.resetEntityInitialier();
}
Expand Down Expand Up @@ -599,7 +594,7 @@
@Override
public void addEntity(EntityUniqueKey euk, Object entity) {
if ( entitiesByUniqueKey == null ) {
entitiesByUniqueKey = CollectionHelper.mapOfSize( INIT_COLL_SIZE );
entitiesByUniqueKey = mapOfSize( INIT_COLL_SIZE );
}
entitiesByUniqueKey.put( euk, entity );
}
Expand Down Expand Up @@ -733,7 +728,8 @@

@Override
public boolean containsCollection(PersistentCollection<?> collection) {
return collectionEntries != null && collectionEntries.containsKey( collection.$$_hibernate_getInstanceId(), collection );
return collectionEntries != null
&& collectionEntries.containsKey( collection.$$_hibernate_getInstanceId(), collection );
}

@Override
Expand Down Expand Up @@ -818,9 +814,8 @@
final LazyInitializer lazyInitializer = extractLazyInitializer( maybeProxy );
if ( lazyInitializer != null ) {
if ( lazyInitializer.isUninitialized() ) {
throw new PersistentObjectException(
"object was an uninitialized proxy for " + lazyInitializer.getEntityName()
);
throw new PersistentObjectException( "object was an uninitialized proxy for "
+ lazyInitializer.getEntityName() );
}
//unwrap the object and return
return lazyInitializer.getImplementation();
Expand All @@ -839,10 +834,11 @@
return lazyInitializer.getImplementation();
}
else if ( isPersistentAttributeInterceptable( maybeProxy ) ) {
final PersistentAttributeInterceptable interceptable = asPersistentAttributeInterceptable( maybeProxy );
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
( (EnhancementAsProxyLazinessInterceptor) interceptor ).forceInitialize( maybeProxy, null );
final PersistentAttributeInterceptor interceptor =
asPersistentAttributeInterceptable( maybeProxy )
.$$_hibernate_getInterceptor();
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor lazinessInterceptor ) {
lazinessInterceptor.forceInitialize( maybeProxy, null );
}
return maybeProxy;
}
Expand Down Expand Up @@ -893,7 +889,8 @@


// Otherwise, create the narrowed proxy
final HibernateProxy narrowedProxy = asHibernateProxy( persister.createProxy( key.getIdentifier(), session ) );
final HibernateProxy narrowedProxy =
asHibernateProxy( persister.createProxy( key.getIdentifier(), session ) );
// set the read-only/modifiable mode in the new proxy to what it was in the original proxy
narrowedProxy.getHibernateLazyInitializer().setReadOnly( lazyInitializer.isReadOnly() );
return narrowedProxy;
Expand Down Expand Up @@ -1309,9 +1306,9 @@
@Override
public Map<EntityKey,Object> getEntitiesByKey() {
if ( entitiesByKey == null ) {
return Collections.emptyMap();
return emptyMap();
}
final HashMap<EntityKey, Object> result = CollectionHelper.mapOfSize( entitiesByKey.size() );
final HashMap<EntityKey, Object> result = mapOfSize( entitiesByKey.size() );
for ( Entry<EntityKey, EntityHolderImpl> entry : entitiesByKey.entrySet() ) {
if ( entry.getValue().entity != null ) {
result.put( entry.getKey(), entry.getValue().entity );
Expand All @@ -1330,7 +1327,7 @@
@Override
public Map<EntityKey, Object> getOrInitializeEntitySnapshotsByKey() {
if ( entitySnapshotsByKey == null ) {
entitySnapshotsByKey = CollectionHelper.mapOfSize( INIT_COLL_SIZE );
entitySnapshotsByKey = mapOfSize( INIT_COLL_SIZE );
}
return entitySnapshotsByKey;
}
Expand Down Expand Up @@ -1384,11 +1381,6 @@
return entityEntryContext.getNumberOfManagedEntities();
}

// @Override
// public Map getEntityEntries() {
// return null;
// }

/**
* @deprecated We should not expose this directly: the other accessors that have been created as a replacement
* have better chances of skipping initializing this map, which is a good performance improvement.
Expand Down Expand Up @@ -1420,7 +1412,7 @@

@Override
public Map<CollectionKey,PersistentCollection<?>> getCollectionsByKey() {
return collectionsByKey == null ? Collections.emptyMap() : collectionsByKey;
return collectionsByKey == null ? emptyMap() : collectionsByKey;
}

@Override
Expand Down Expand Up @@ -1997,7 +1989,7 @@
LOG.trace( "Starting deserialization of [" + count + "] entitiesByUniqueKey entries" );
}
if ( count != 0 ) {
rtn.entitiesByUniqueKey = CollectionHelper.mapOfSize(Math.max(count, INIT_COLL_SIZE));
rtn.entitiesByUniqueKey = mapOfSize(Math.max(count, INIT_COLL_SIZE));
for ( int i = 0; i < count; i++ ) {
rtn.entitiesByUniqueKey.put( EntityUniqueKey.deserialize( ois, session ), ois.readObject() );
}
Expand All @@ -2007,7 +1999,7 @@
if ( traceEnabled ) {
LOG.trace( "Starting deserialization of [" + count + "] entitySnapshotsByKey entries" );
}
rtn.entitySnapshotsByKey = CollectionHelper.mapOfSize(Math.max(count, INIT_COLL_SIZE));
rtn.entitySnapshotsByKey = mapOfSize(Math.max(count, INIT_COLL_SIZE));
for ( int i = 0; i < count; i++ ) {
rtn.entitySnapshotsByKey.put( EntityKey.deserialize( ois, sfi ), ois.readObject() );
}
Expand All @@ -2018,7 +2010,7 @@
if ( traceEnabled ) {
LOG.trace( "Starting deserialization of [" + count + "] entitiesByKey entries" );
}
rtn.entitiesByKey = CollectionHelper.mapOfSize(Math.max(count, INIT_COLL_SIZE));
rtn.entitiesByKey = mapOfSize(Math.max(count, INIT_COLL_SIZE));
for ( int i = 0; i < count; i++ ) {
final EntityKey ek = EntityKey.deserialize( ois, sfi );
final EntityPersister persister = sfi.getMappingMetamodel().getEntityDescriptor( (String) ois.readObject() );
Expand Down Expand Up @@ -2048,7 +2040,7 @@
if ( traceEnabled ) {
LOG.trace( "Starting deserialization of [" + count + "] collectionsByKey entries" );
}
rtn.collectionsByKey = CollectionHelper.mapOfSize(Math.max(count, INIT_COLL_SIZE));
rtn.collectionsByKey = mapOfSize(Math.max(count, INIT_COLL_SIZE));
for ( int i = 0; i < count; i++ ) {
rtn.collectionsByKey.put(
CollectionKey.deserialize( ois, session ),
Expand Down Expand Up @@ -2230,7 +2222,7 @@
@Override
public PersistentCollection<?> addCollectionByKey(CollectionKey collectionKey, PersistentCollection<?> persistentCollection) {
if ( collectionsByKey == null ) {
collectionsByKey = CollectionHelper.mapOfSize( INIT_COLL_SIZE );
collectionsByKey = mapOfSize( INIT_COLL_SIZE );
}
return collectionsByKey.put( collectionKey, persistentCollection );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
*/
package org.hibernate.engine.spi;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;

import jakarta.persistence.metamodel.EntityType;
import org.hibernate.CacheMode;
import org.hibernate.Filter;
import org.hibernate.FlushMode;
Expand Down Expand Up @@ -1115,6 +1117,26 @@ public LobHelper getLobHelper() {
return delegate.getLobHelper();
}

@Override
public Collection<?> getManagedEntities() {
return delegate.getManagedEntities();
}

@Override
public Collection<?> getManagedEntities(String entityName) {
return delegate.getManagedEntities( entityName );
}

@Override
public <E> Collection<E> getManagedEntities(Class<E> entityType) {
return delegate.getManagedEntities( entityType );
}

@Override
public <E> Collection<E> getManagedEntities(EntityType<E> entityType) {
return delegate.getManagedEntities( entityType );
}

@Override
public void addEventListeners(SessionEventListener... listeners) {
delegate.addEventListeners( listeners );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
*/
package org.hibernate.engine.spi;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import jakarta.persistence.metamodel.EntityType;
import org.hibernate.CacheMode;
import org.hibernate.Filter;
import org.hibernate.FlushMode;
Expand Down Expand Up @@ -420,6 +422,26 @@ public LobHelper getLobHelper() {
return this.lazySession.get().getLobHelper();
}

@Override
public Collection<?> getManagedEntities() {
return this.lazySession.get().getManagedEntities();
}

@Override
public Collection<?> getManagedEntities(String entityName) {
return this.lazySession.get().getManagedEntities( entityName );
}

@Override
public <E> Collection<E> getManagedEntities(Class<E> entityType) {
return this.lazySession.get().getManagedEntities( entityType );
}

@Override
public <E> Collection<E> getManagedEntities(EntityType<E> entityType) {
return this.lazySession.get().getManagedEntities( entityType );
}

@Override
public SharedSessionBuilder sessionWithOptions() {
return this.lazySession.get().sessionWithOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.sql.Connection;
import java.sql.NClob;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -24,6 +25,7 @@

import jakarta.persistence.PessimisticLockScope;
import jakarta.persistence.Timeout;
import jakarta.persistence.metamodel.EntityType;
import org.hibernate.BatchSize;
import org.hibernate.CacheMode;
import org.hibernate.ConnectionAcquisitionMode;
Expand Down Expand Up @@ -64,6 +66,7 @@
import org.hibernate.engine.spi.ActionQueue.TransactionCompletionProcesses;
import org.hibernate.engine.spi.EffectiveEntityGraph;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityHolder;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.PersistenceContext;
Expand Down Expand Up @@ -106,6 +109,7 @@
import org.hibernate.event.spi.ReplicateEvent;
import org.hibernate.event.spi.ReplicateEventListener;
import org.hibernate.loader.internal.CacheLoadHelper;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.resource.transaction.spi.TransactionObserver;
import org.hibernate.event.monitor.spi.EventMonitor;
Expand Down Expand Up @@ -2983,6 +2987,38 @@ public Metamodel getMetamodel() {
return getFactory().getJpaMetamodel();
}

@Override
public Collection<?> getManagedEntities() {
return persistenceContext.getEntityHoldersByKey()
.values().stream().map( EntityHolder::getManagedObject )
.toList();
}

@Override
public Collection<?> getManagedEntities(String entityName) {
return persistenceContext.getEntityHoldersByKey().entrySet().stream()
.filter( entry -> entry.getKey().getEntityName().equals( entityName ) )
.map( entry -> entry.getValue().getManagedObject() )
.toList();
}

@Override
public <E> Collection<E> getManagedEntities(Class<E> entityType) {
return persistenceContext.getEntityHoldersByKey().entrySet().stream()
.filter( entry -> entry.getKey().getPersister().getMappedClass().equals( entityType ) )
.map( entry -> (E) entry.getValue().getManagedObject() )
.toList();
}

@Override
public <E> Collection<E> getManagedEntities(EntityType<E> entityType) {
final String entityName = ( (EntityDomainType<E>) entityType ).getHibernateEntityName();
return persistenceContext.getEntityHoldersByKey().entrySet().stream()
.filter( entry -> entry.getKey().getEntityName().equals( entityName ) )
.map( entry -> (E) entry.getValue().getManagedObject() )
.toList();
}

/**
* Used by JDK serialization...
*
Expand Down
Loading
Loading