|
41 | 41 | import org.hibernate.event.spi.*; |
42 | 42 | import org.hibernate.event.spi.LoadEventListener.LoadType; |
43 | 43 | import org.hibernate.graph.GraphSemantic; |
44 | | -import org.hibernate.graph.RootGraph; |
45 | 44 | import org.hibernate.graph.spi.RootGraphImplementor; |
| 45 | +import org.hibernate.internal.find.FindByKeyOperation; |
| 46 | +import org.hibernate.internal.find.FindMultipleByKeyOperation; |
46 | 47 | import org.hibernate.internal.util.ExceptionHelper; |
47 | 48 | import org.hibernate.jpa.internal.LegacySpecHelper; |
48 | 49 | import org.hibernate.jpa.internal.util.ConfigurationHelper; |
@@ -990,30 +991,45 @@ else if ( option instanceof RemovalsMode ) { |
990 | 991 | } |
991 | 992 |
|
992 | 993 | @Override |
993 | | - public <E> List<E> findMultiple(Class<E> entityType, List<?> ids, FindOption... options) { |
994 | | - final var loadAccess = byMultipleIds( entityType ); |
995 | | - final boolean isFindByNaturalId = setMultiIdentifierLoadAccessOptions( options, loadAccess ); |
996 | | - if ( isFindByNaturalId ) { |
997 | | - return findMultipleByNaturalId( entityType, ids, options ); |
998 | | - } |
999 | | - return loadAccess.multiLoad( ids ); |
| 994 | + public <E> List<E> findMultiple(Class<E> entityType, List<?> keys, FindOption... options) { |
| 995 | + //noinspection unchecked |
| 996 | + return findMultiple( |
| 997 | + requireEntityPersister( entityType ), |
| 998 | + loadQueryInfluencers.getEffectiveEntityGraph().getSemantic(), |
| 999 | + (RootGraphImplementor<E>) loadQueryInfluencers.getEffectiveEntityGraph().getGraph(), |
| 1000 | + (List<Object>) keys, |
| 1001 | + options |
| 1002 | + ); |
| 1003 | + } |
| 1004 | + |
| 1005 | + private <E> List<E> findMultiple( |
| 1006 | + EntityPersister entityDescriptor, |
| 1007 | + GraphSemantic graphSemantic, |
| 1008 | + RootGraphImplementor<E> rootGraph, |
| 1009 | + List<Object> keys, |
| 1010 | + FindOption... options) { |
| 1011 | + final var operation = new FindMultipleByKeyOperation<E>( |
| 1012 | + entityDescriptor, |
| 1013 | + lockOptions, |
| 1014 | + getCacheMode(), |
| 1015 | + isDefaultReadOnly(), |
| 1016 | + getFactory(), |
| 1017 | + options |
| 1018 | + ); |
| 1019 | + return operation.performFind( keys, graphSemantic, rootGraph, this ); |
1000 | 1020 | } |
1001 | 1021 |
|
1002 | 1022 | @Override |
1003 | | - public <E> List<E> findMultiple(EntityGraph<E> entityGraph, List<?> ids, FindOption... options) { |
1004 | | - final var rootGraph = (RootGraph<E>) entityGraph; |
| 1023 | + public <E> List<E> findMultiple(EntityGraph<E> entityGraph, List<?> keys, FindOption... options) { |
| 1024 | + final var rootGraph = (RootGraphImplementor<E>) entityGraph; |
1005 | 1025 | final var type = rootGraph.getGraphedType(); |
1006 | | - final MultiIdentifierLoadAccess<E> loadAccess = |
1007 | | - switch ( type.getRepresentationMode() ) { |
1008 | | - case MAP -> byMultipleIds( type.getTypeName() ); |
1009 | | - case POJO -> byMultipleIds( type.getJavaType() ); |
1010 | | - }; |
1011 | | - loadAccess.withLoadGraph( rootGraph ); |
1012 | | - final boolean isFindByNaturalId = setMultiIdentifierLoadAccessOptions( options, loadAccess ); |
1013 | | - if ( isFindByNaturalId ) { |
1014 | | - throw new UnsupportedOperationException( "Find by natural-id with entity-graph is not supported" ); |
1015 | | - } |
1016 | | - return loadAccess.multiLoad( ids ); |
| 1026 | + final var entityDescriptor = switch ( type.getRepresentationMode() ) { |
| 1027 | + case POJO -> requireEntityPersister( type.getJavaType() ); |
| 1028 | + case MAP -> requireEntityPersister( type.getTypeName() ); |
| 1029 | + }; |
| 1030 | + |
| 1031 | + //noinspection unchecked |
| 1032 | + return findMultiple( entityDescriptor, GraphSemantic.LOAD, rootGraph, (List<Object>) keys, options ); |
1017 | 1033 | } |
1018 | 1034 |
|
1019 | 1035 | @Override |
@@ -2330,28 +2346,22 @@ else if ( option instanceof FindMultipleOption findMultipleOption ) { |
2330 | 2346 |
|
2331 | 2347 | @Override |
2332 | 2348 | public <T> T find(Class<T> entityClass, Object key, FindOption... options) { |
2333 | | - final IdentifierLoadAccessImpl<T> loadAccess = byId( entityClass ); |
2334 | | - final boolean isFindByNaturalId = setLoadAccessOptions( options, loadAccess ); |
2335 | | - if ( isFindByNaturalId ) { |
2336 | | - return findByNaturalId( entityClass, key, options ); |
2337 | | - } |
2338 | | - return loadAccess.load( key ); |
| 2349 | + //noinspection unchecked |
| 2350 | + return (T) byKey( requireEntityPersister( entityClass ), options ).performFind( key, this ); |
2339 | 2351 | } |
2340 | 2352 |
|
2341 | 2353 | @Override |
2342 | | - public <T> T find(EntityGraph<T> entityGraph, Object primaryKey, FindOption... options) { |
2343 | | - final var graph = (RootGraph<T>) entityGraph; |
| 2354 | + public <T> T find(EntityGraph<T> entityGraph, Object key, FindOption... options) { |
| 2355 | + final var graph = (RootGraphImplementor<T>) entityGraph; |
2344 | 2356 | final var type = graph.getGraphedType(); |
2345 | | - final IdentifierLoadAccessImpl<T> loadAccess = |
2346 | | - switch ( type.getRepresentationMode() ) { |
2347 | | - case MAP -> byId( type.getTypeName() ); |
2348 | | - case POJO -> byId( type.getJavaType() ); |
2349 | | - }; |
2350 | | - final boolean isFindByNaturalId = setLoadAccessOptions( options, loadAccess ); |
2351 | | - if ( isFindByNaturalId ) { |
2352 | | - throw new UnsupportedOperationException( "Find by natural-id with entity-graph is not supported" ); |
2353 | | - } |
2354 | | - return loadAccess.withLoadGraph( graph ).load( primaryKey ); |
| 2357 | + |
| 2358 | + final EntityPersister entityDescriptor = switch ( type.getRepresentationMode() ) { |
| 2359 | + case POJO -> requireEntityPersister( type.getJavaType() ); |
| 2360 | + case MAP -> requireEntityPersister( type.getTypeName() ); |
| 2361 | + }; |
| 2362 | + |
| 2363 | + //noinspection unchecked |
| 2364 | + return (T) byKey( entityDescriptor, GraphSemantic.LOAD, graph, options ).performFind( key, this ); |
2355 | 2365 | } |
2356 | 2366 |
|
2357 | 2367 | // Hibernate Reactive may need to use this |
@@ -2414,115 +2424,34 @@ private void checkTransactionNeededForUpdateOperation() { |
2414 | 2424 | } |
2415 | 2425 |
|
2416 | 2426 | @Override |
2417 | | - public Object find(String entityName, Object primaryKey) { |
2418 | | - final IdentifierLoadAccessImpl<?> loadAccess = byId( entityName ); |
2419 | | - return loadAccess.load( primaryKey ); |
2420 | | - } |
2421 | | - |
2422 | | - @Override |
2423 | | - public Object find(String entityName, Object primaryKey, FindOption... options) { |
2424 | | - final IdentifierLoadAccessImpl<?> loadAccess = byId( entityName ); |
2425 | | - final boolean isFindByNaturalId = setLoadAccessOptions( options, loadAccess ); |
2426 | | - if ( isFindByNaturalId ) { |
2427 | | - return findByNaturalId( entityName, primaryKey, options ); |
2428 | | - } |
2429 | | - return loadAccess.load( primaryKey ); |
2430 | | - } |
2431 | | - |
2432 | | - @Override |
2433 | | - public <T> T findByNaturalId(Class<T> entityType, Object naturalId, FindOption... options) { |
2434 | | - final SimpleNaturalIdLoadAccessImpl<T> access = (SimpleNaturalIdLoadAccessImpl<T>) bySimpleNaturalId( entityType ); |
2435 | | - setOptions( options, access ); |
2436 | | - return access.load( naturalId ); |
2437 | | - } |
2438 | | - |
2439 | | - private <T> void setOptions(FindOption[] options, SimpleNaturalIdLoadAccessImpl<T> access) { |
2440 | | - for ( FindOption option : options ) { |
2441 | | - if ( option instanceof FindBy findBy ) { |
2442 | | - if ( findBy == FindBy.ID ) { |
2443 | | - throw new IllegalArgumentException( "Cannot use FindBy#ID with findByNaturalId" ); |
2444 | | - } |
2445 | | - } |
2446 | | - else if ( option instanceof LockMode lockMode ) { |
2447 | | - access.with( lockMode ); |
2448 | | - } |
2449 | | - else if ( option instanceof LockModeType lockModeType ) { |
2450 | | - access.with( LockMode.fromJpaLockMode( lockModeType ) ); |
2451 | | - } |
2452 | | - else if ( option instanceof Locking.Scope scope ) { |
2453 | | - access.with( scope ); |
2454 | | - } |
2455 | | - else if ( option instanceof PessimisticLockScope scope ) { |
2456 | | - access.with( Locking.Scope.fromJpaScope( scope ) ); |
2457 | | - } |
2458 | | - else if ( option instanceof Timeout timeout ) { |
2459 | | - access.with( timeout ); |
2460 | | - } |
2461 | | - else { |
2462 | | - throw new IllegalArgumentException( "Illegal option: " + option ); |
2463 | | - } |
2464 | | - } |
2465 | | - } |
2466 | | - |
2467 | | - @Override |
2468 | | - public Object findByNaturalId(String entityName, Object naturalId, FindOption... options) { |
2469 | | - final SimpleNaturalIdLoadAccessImpl<?> access = (SimpleNaturalIdLoadAccessImpl<?>) bySimpleNaturalId( entityName ); |
2470 | | - setOptions( options, access ); |
2471 | | - return access.load( naturalId ); |
| 2427 | + public Object find(String entityName, Object key) { |
| 2428 | + return byKey( requireEntityPersister( entityName ) ).performFind( key, this ); |
2472 | 2429 | } |
2473 | 2430 |
|
2474 | 2431 | @Override |
2475 | | - public <T> List<T> findMultipleByNaturalId(Class<T> entityType, List<?> naturalIds, FindOption... options) { |
2476 | | - final NaturalIdMultiLoadAccessStandard<T> access = (NaturalIdMultiLoadAccessStandard<T>) byMultipleNaturalId( entityType ); |
2477 | | - setOptions( options, access ); |
2478 | | - return access.multiLoad( naturalIds ); |
| 2432 | + public Object find(String entityName, Object key, FindOption... options) { |
| 2433 | + return byKey( requireEntityPersister( entityName ), options ).performFind( key, this ); |
2479 | 2434 | } |
2480 | 2435 |
|
2481 | | - private <T> void setOptions(FindOption[] options, NaturalIdMultiLoadAccessStandard<T> access) { |
2482 | | - for ( FindOption option : options ) { |
2483 | | - if ( option instanceof FindBy findBy ) { |
2484 | | - if ( findBy == FindBy.ID ) { |
2485 | | - throw new IllegalArgumentException( "Cannot use FindBy#ID with findMultipleByNaturalId" ); |
2486 | | - } |
2487 | | - } |
2488 | | - else if ( option instanceof LockMode lockMode ) { |
2489 | | - access.with( lockMode ); |
2490 | | - } |
2491 | | - else if ( option instanceof LockModeType lockModeType ) { |
2492 | | - access.with( LockMode.fromJpaLockMode( lockModeType ) ); |
2493 | | - } |
2494 | | - else if ( option instanceof Locking.Scope scope ) { |
2495 | | - access.with( scope ); |
2496 | | - } |
2497 | | - else if ( option instanceof PessimisticLockScope scope ) { |
2498 | | - access.with( Locking.Scope.fromJpaScope( scope ) ); |
2499 | | - } |
2500 | | - else if ( option instanceof Timeout timeout ) { |
2501 | | - access.with( timeout ); |
2502 | | - } |
2503 | | - else if ( option instanceof CacheMode cacheMode ) { |
2504 | | - access.with( cacheMode ); |
2505 | | - } |
2506 | | - else if ( option instanceof BatchSize batchSize ) { |
2507 | | - access.withBatchSize( batchSize.batchSize() ); |
2508 | | - } |
2509 | | - else if ( option instanceof RemovalsMode removalsMode ) { |
2510 | | - access.with( removalsMode ); |
2511 | | - } |
2512 | | - else if ( option instanceof OrderingMode orderingMode ) { |
2513 | | - access.with( orderingMode ); |
2514 | | - } |
2515 | | - else { |
2516 | | - throw new IllegalArgumentException( "Illegal option: " + option ); |
2517 | | - } |
2518 | | - } |
| 2436 | + private <T> FindByKeyOperation<T> byKey(EntityPersister entityDescriptor, FindOption... options) { |
| 2437 | + return byKey( entityDescriptor, null, null, options ); |
2519 | 2438 | } |
2520 | 2439 |
|
2521 | | - @Override |
2522 | | - public List<Object> findMultipleByNaturalId(String entityName, List<?> naturalIds, FindOption... options) { |
2523 | | - final NaturalIdMultiLoadAccessStandard<Object> access = (NaturalIdMultiLoadAccessStandard<Object>) byMultipleNaturalId( entityName ); |
2524 | | - setOptions( options, access ); |
2525 | | - return access.multiLoad( naturalIds ); |
| 2440 | + private <T> FindByKeyOperation<T> byKey( |
| 2441 | + EntityPersister entityDescriptor, |
| 2442 | + GraphSemantic graphSemantic, |
| 2443 | + RootGraphImplementor<?> rootGraph, |
| 2444 | + FindOption... options) { |
| 2445 | + return new FindByKeyOperation<>( |
| 2446 | + entityDescriptor, |
| 2447 | + graphSemantic, |
| 2448 | + rootGraph, |
| 2449 | + lockOptions, |
| 2450 | + getCacheMode(), |
| 2451 | + isReadOnly(), |
| 2452 | + getFactory(), |
| 2453 | + options |
| 2454 | + ); |
2526 | 2455 | } |
2527 | 2456 |
|
2528 | 2457 | @Override |
|
0 commit comments