|
33 | 33 | import org.hibernate.engine.spi.EntityKey;
|
34 | 34 | import org.hibernate.engine.spi.LoadQueryInfluencers;
|
35 | 35 | import org.hibernate.engine.spi.PersistenceContext;
|
| 36 | +import org.hibernate.engine.spi.SessionImplementor; |
36 | 37 | import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
37 | 38 | import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper;
|
38 | 39 | import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
|
|
75 | 76 | import org.hibernate.id.IdentifierGenerationException;
|
76 | 77 | import org.hibernate.loader.ast.internal.LoaderHelper;
|
77 | 78 | import org.hibernate.loader.ast.spi.CascadingFetchProfile;
|
| 79 | +import org.hibernate.loader.ast.spi.MultiIdLoadOptions; |
78 | 80 | import org.hibernate.loader.internal.CacheLoadHelper;
|
79 | 81 | import org.hibernate.persister.collection.CollectionPersister;
|
80 | 82 | import org.hibernate.persister.entity.EntityPersister;
|
81 | 83 | import org.hibernate.proxy.LazyInitializer;
|
82 |
| -import org.hibernate.query.criteria.JpaCriteriaQuery; |
83 |
| -import org.hibernate.query.criteria.JpaRoot; |
84 | 84 | import org.hibernate.stat.spi.StatisticsImplementor;
|
85 | 85 | import org.hibernate.tuple.entity.EntityMetamodel;
|
86 | 86 |
|
87 | 87 | import jakarta.persistence.EntityGraph;
|
88 | 88 | import jakarta.transaction.SystemException;
|
89 | 89 |
|
90 |
| -import static java.util.Collections.unmodifiableList; |
91 | 90 | import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
|
92 | 91 | import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
|
93 | 92 | import static org.hibernate.engine.internal.PersistenceContexts.createPersistenceContext;
|
|
129 | 128 | public class StatelessSessionImpl extends AbstractSharedSessionContract implements StatelessSession {
|
130 | 129 | private static final CoreMessageLogger LOG = CoreLogging.messageLogger( StatelessSessionImpl.class );
|
131 | 130 |
|
| 131 | + public static final MultiIdLoadOptions MULTI_ID_LOAD_OPTIONS = new MultiIdLoadOptions() { |
| 132 | + @Override |
| 133 | + public boolean isSessionCheckingEnabled() { |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public boolean isSecondLevelCacheCheckingEnabled() { |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public Boolean getReadOnly(SessionImplementor session) { |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public boolean isReturnOfDeletedEntitiesEnabled() { |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public boolean isOrderReturnEnabled() { |
| 154 | + return true; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public LockOptions getLockOptions() { |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public Integer getBatchSize() { |
| 164 | + return null; |
| 165 | + } |
| 166 | + }; |
| 167 | + |
132 | 168 | private final LoadQueryInfluencers influencers;
|
133 | 169 | private final PersistenceContext temporaryPersistenceContext;
|
134 | 170 | private final boolean connectionProvided;
|
@@ -776,43 +812,47 @@ public <T> List<T> getMultiple(Class<T> entityClass, List<?> ids) {
|
776 | 812 |
|
777 | 813 | final EntityPersister persister = requireEntityPersister( entityClass.getName() );
|
778 | 814 |
|
779 |
| - final List<Object> uncachedIds; |
780 |
| - final List<T> list = new ArrayList<>( ids.size() ); |
781 |
| - if ( persister.canReadFromCache() ) { |
782 |
| - uncachedIds = new ArrayList<>( ids.size() ); |
783 |
| - for (Object id : ids) { |
784 |
| - final Object cachedEntity = |
785 |
| - loadFromSecondLevelCache( persister, generateEntityKey( id, persister ), null, LockMode.NONE ); |
786 |
| - if ( cachedEntity == null ) { |
787 |
| - uncachedIds.add( id ); |
788 |
| - list.add( null ); |
789 |
| - } |
790 |
| - else { |
791 |
| - //noinspection unchecked |
792 |
| - list.add( (T) cachedEntity ); |
793 |
| - } |
794 |
| - } |
795 |
| - } |
796 |
| - else { |
797 |
| - uncachedIds = unmodifiableList(ids); |
798 |
| - for (int i = 0; i < ids.size(); i++) { |
799 |
| - list.add( null ); |
800 |
| - } |
801 |
| - } |
802 |
| - |
803 |
| - final JpaCriteriaQuery<T> query = getCriteriaBuilder().createQuery(entityClass); |
804 |
| - final JpaRoot<T> from = query.from(entityClass); |
805 |
| - query.where( from.get( persister.getIdentifierPropertyName() ).in(uncachedIds) ); |
806 |
| - final List<T> resultList = createSelectionQuery(query).getResultList(); |
807 |
| - for (int i = 0; i < ids.size(); i++) { |
808 |
| - if ( list.get(i) == null ) { |
809 |
| - final Object id = ids.get(i); |
810 |
| - list.set( i, resultList.stream() |
811 |
| - .filter( entity -> entity != null && persister.getIdentifier( entity, this ).equals(id) ) |
812 |
| - .findFirst().orElse( null ) ); |
813 |
| - } |
814 |
| - } |
815 |
| - return list; |
| 815 | + final List<?> results = persister.multiLoad( ids.toArray(), this, MULTI_ID_LOAD_OPTIONS ); |
| 816 | + //noinspection unchecked |
| 817 | + return (List<T>) results; |
| 818 | + |
| 819 | +// final List<Object> uncachedIds; |
| 820 | +// final List<T> list = new ArrayList<>( ids.size() ); |
| 821 | +// if ( persister.canReadFromCache() ) { |
| 822 | +// uncachedIds = new ArrayList<>( ids.size() ); |
| 823 | +// for (Object id : ids) { |
| 824 | +// final Object cachedEntity = |
| 825 | +// loadFromSecondLevelCache( persister, generateEntityKey( id, persister ), null, LockMode.NONE ); |
| 826 | +// if ( cachedEntity == null ) { |
| 827 | +// uncachedIds.add( id ); |
| 828 | +// list.add( null ); |
| 829 | +// } |
| 830 | +// else { |
| 831 | +// //noinspection unchecked |
| 832 | +// list.add( (T) cachedEntity ); |
| 833 | +// } |
| 834 | +// } |
| 835 | +// } |
| 836 | +// else { |
| 837 | +// uncachedIds = unmodifiableList(ids); |
| 838 | +// for (int i = 0; i < ids.size(); i++) { |
| 839 | +// list.add( null ); |
| 840 | +// } |
| 841 | +// } |
| 842 | +// |
| 843 | +// final JpaCriteriaQuery<T> query = getCriteriaBuilder().createQuery(entityClass); |
| 844 | +// final JpaRoot<T> from = query.from(entityClass); |
| 845 | +// query.where( from.get( persister.getIdentifierPropertyName() ).in(uncachedIds) ); |
| 846 | +// final List<T> resultList = createSelectionQuery(query).getResultList(); |
| 847 | +// for (int i = 0; i < ids.size(); i++) { |
| 848 | +// if ( list.get(i) == null ) { |
| 849 | +// final Object id = ids.get(i); |
| 850 | +// list.set( i, resultList.stream() |
| 851 | +// .filter( entity -> entity != null && persister.getIdentifier( entity, this ).equals(id) ) |
| 852 | +// .findFirst().orElse( null ) ); |
| 853 | +// } |
| 854 | +// } |
| 855 | +// return list; |
816 | 856 | }
|
817 | 857 |
|
818 | 858 | @Override
|
|
0 commit comments