Skip to content

Commit 364b04f

Browse files
committed
HSEARCH-5442 More updates to address ORM changes
1 parent 47a0a55 commit 364b04f

File tree

5 files changed

+77
-15
lines changed

5 files changed

+77
-15
lines changed

build/config/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,16 @@
220220
If no prefix is specified then rule is considered to be both public and internal at the same time.
221221
-->
222222

223+
<argument>^org\.hibernate\.Timeouts$</argument>
224+
223225
<argument>^org\.hibernate\.query\.Query$</argument>
224226
<argument>^org\.hibernate\.query\.SelectionQuery$</argument>
225227
<argument>^org\.hibernate\.query\.MutationQuery$</argument>
226228
<argument>^org\.hibernate\.query\.BindableType$</argument>
227229
<argument>^org\.hibernate\.query\.BindingContext$</argument>
228230

231+
<argument>^org\.hibernate\.dialect\.lock\.spi\.LockingSupport$</argument>
232+
229233
<argument>^org\.hibernate\.metamodel\.MappingMetamodel$</argument>
230234
<argument>^org\.hibernate\.metamodel\.mapping\.Bindable$</argument>
231235
<argument>^org\.hibernate\.metamodel\.mapping\.SelectableMapping$</argument>

integrationtest/mapper/orm-outbox-polling/src/test/java/org/hibernate/search/integrationtest/mapper/orm/outboxpolling/automaticindexing/OutboxPollingAutomaticIndexingEdgeCasesIT.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.hibernate.LockMode;
2222
import org.hibernate.SessionFactory;
23+
import org.hibernate.Timeouts;
24+
import org.hibernate.dialect.lock.spi.LockTimeoutType;
2325
import org.hibernate.engine.spi.SessionFactoryImplementor;
2426
import org.hibernate.search.integrationtest.mapper.orm.outboxpolling.testsupport.util.OutboxEventFilter;
2527
import org.hibernate.search.integrationtest.mapper.orm.outboxpolling.testsupport.util.TestingOutboxPollingInternalConfigurer;
@@ -201,7 +203,8 @@ void addIndexedAndContained_addAndUpdateEventsProcessedInDifferentBatches() {
201203
@Test
202204
void lockedEventRowRetries() {
203205
assumeTrue(
204-
sessionFactory.unwrap( SessionFactoryImplementor.class ).getJdbcServices().getDialect().supportsSkipLocked(),
206+
sessionFactory.unwrap( SessionFactoryImplementor.class ).getJdbcServices().getDialect().getLockingSupport()
207+
.getMetadata().getLockTimeoutType( Timeouts.SKIP_LOCKED ) == LockTimeoutType.QUERY,
205208
"This test only make sense if skip locked rows is supported by the underlying DB. " +
206209
"Otherwise the locking will trow an exception and the batch will be just reprocessed without a retry of locking events." );
207210

integrationtest/mapper/orm/src/test/java/org/hibernate/search/integrationtest/mapper/orm/automaticindexing/SessionIndexingPlanFilterIT.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,16 +537,17 @@ void dynamicTypeByNameDirectPersistUpdateDelete() {
537537
Search.session( session ).indexingPlanFilter( ctx -> ctx.exclude( DYNAMIC_BASE_TYPE_A ) );
538538

539539
@SuppressWarnings("unchecked")
540-
Map<String, Object> entity1 = (Map<String, Object>) session.byId( DYNAMIC_SUBTYPE_B ).load( 1 );
540+
Map<String, Object> entity1 = (Map<String, Object>) session
541+
.find( session.getSessionFactory().createGraphForDynamicEntity( DYNAMIC_SUBTYPE_B ), 1 );
541542
entity1.put( "propertyOfA", "updatedValue" );
542543
} );
543544
backendMock.verifyExpectationsMet();
544545

545546
with( sessionFactory ).runInTransaction( session -> {
546547
Search.session( session ).indexingPlanFilter( ctx -> ctx.exclude( DYNAMIC_BASE_TYPE_A ) );
547548

548-
session.remove( session.byId( DYNAMIC_SUBTYPE_B ).load( 1 ) );
549-
session.remove( session.byId( DYNAMIC_SUBTYPE_C ).load( 2 ) );
549+
session.remove( session.find( session.getSessionFactory().createGraphForDynamicEntity( DYNAMIC_SUBTYPE_B ), 1 ) );
550+
session.remove( session.find( session.getSessionFactory().createGraphForDynamicEntity( DYNAMIC_SUBTYPE_C ), 2 ) );
550551

551552
} );
552553
backendMock.verifyExpectationsMet();

mapper/orm-outbox-polling/src/main/java/org/hibernate/search/mapper/orm/outboxpolling/event/impl/OutboxEventLoader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import org.hibernate.LockMode;
1818
import org.hibernate.Session;
19+
import org.hibernate.Timeouts;
1920
import org.hibernate.dialect.Dialect;
21+
import org.hibernate.dialect.lock.spi.LockTimeoutType;
2022
import org.hibernate.query.Query;
2123
import org.hibernate.search.mapper.orm.outboxpolling.logging.impl.OutboxPollingEventsLog;
2224
import org.hibernate.search.util.common.spi.ToStringTreeAppendable;
@@ -44,7 +46,7 @@ final class OutboxEventLoader implements ToStringTreeAppendable {
4446
// That's possible because event processing is not sensitive to processing order,
4547
// so we can afford to just skip events that are already locked,
4648
// and process them later when they are no longer locked.
47-
if ( dialect.supportsSkipLocked() ) {
49+
if ( dialect.getLockingSupport().getMetadata().getLockTimeoutType( Timeouts.SKIP_LOCKED ) == LockTimeoutType.QUERY ) {
4850
lockMode = LockMode.UPGRADE_SKIPLOCKED;
4951
}
5052
// If LockMode.UPGRADE_SKIPLOCKED is not supported, we just do basic locking and hope for the best

mapper/orm/src/main/java/org/hibernate/search/mapper/orm/loading/impl/PersistenceContextThenSecondLevelCacheLookupStrategy.java

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
*/
55
package org.hibernate.search.mapper.orm.loading.impl;
66

7+
import java.util.Map;
8+
79
import org.hibernate.ObjectNotFoundException;
810
import org.hibernate.cache.spi.access.EntityDataAccess;
911
import org.hibernate.engine.spi.EntityKey;
1012
import org.hibernate.engine.spi.SessionImplementor;
13+
import org.hibernate.graph.RootGraph;
14+
import org.hibernate.metamodel.RepresentationMode;
1115
import org.hibernate.metamodel.mapping.EntityMappingType;
1216
import org.hibernate.persister.entity.EntityPersister;
1317
import org.hibernate.search.mapper.orm.logging.impl.OrmMiscLog;
@@ -22,7 +26,7 @@
2226
* @author Emmanuel Bernard
2327
*/
2428
@SuppressForbiddenApis(reason = "EntityPersister is needed to retrieve/use EntityDataAccess")
25-
class PersistenceContextThenSecondLevelCacheLookupStrategy
29+
abstract class PersistenceContextThenSecondLevelCacheLookupStrategy
2630
implements EntityLoadingCacheLookupStrategyImplementor {
2731

2832
static EntityLoadingCacheLookupStrategyImplementor create(EntityMappingType entityMappingType,
@@ -37,18 +41,29 @@ static EntityLoadingCacheLookupStrategyImplementor create(EntityMappingType enti
3741
entityPersister.getEntityName() );
3842
return persistenceContextLookupStrategy;
3943
}
40-
return new PersistenceContextThenSecondLevelCacheLookupStrategy(
41-
persistenceContextLookupStrategy,
42-
entityPersister,
43-
cacheAccess,
44-
session
45-
);
44+
45+
if ( RepresentationMode.MAP.equals( entityMappingType.getRepresentationStrategy().getMode() ) ) {
46+
return new DynamicMapPersistenceContextThenSecondLevelCacheLookupStrategy(
47+
persistenceContextLookupStrategy,
48+
entityPersister,
49+
cacheAccess,
50+
session
51+
);
52+
}
53+
else {
54+
return new PojoPersistenceContextThenSecondLevelCacheLookupStrategy(
55+
persistenceContextLookupStrategy,
56+
entityPersister,
57+
cacheAccess,
58+
session
59+
);
60+
}
4661
}
4762

4863
private final EntityLoadingCacheLookupStrategyImplementor persistenceContextLookupStrategy;
49-
private final EntityPersister persister;
5064
private final EntityDataAccess cacheAccess;
51-
private final SessionImplementor session;
65+
protected final EntityPersister persister;
66+
protected final SessionImplementor session;
5267

5368
private PersistenceContextThenSecondLevelCacheLookupStrategy(
5469
EntityLoadingCacheLookupStrategyImplementor persistenceContextLookupStrategy,
@@ -84,12 +99,49 @@ public Object lookup(EntityKey entityKey) {
8499

85100
try {
86101
// This will load the object from the second level cache
87-
return session.byId( persister.getEntityName() ).load( entityKey.getIdentifier() );
102+
return lookupByIdentifier( entityKey );
88103
}
89104
catch (ObjectNotFoundException ignored) {
90105
// Unlikely but needed: an index might be out of sync, and the cache might be as well
91106
// Ignore the exception and handle as a cache miss by returning null
92107
return null;
93108
}
94109
}
110+
111+
protected abstract Object lookupByIdentifier(EntityKey entityKey);
112+
113+
@SuppressForbiddenApis(reason = "EntityPersister is needed to retrieve/use EntityDataAccess")
114+
private static class DynamicMapPersistenceContextThenSecondLevelCacheLookupStrategy
115+
extends PersistenceContextThenSecondLevelCacheLookupStrategy {
116+
117+
private final RootGraph<Map<String, ?>> graphForDynamicEntity;
118+
119+
private DynamicMapPersistenceContextThenSecondLevelCacheLookupStrategy(
120+
EntityLoadingCacheLookupStrategyImplementor persistenceContextLookupStrategy, EntityPersister persister,
121+
EntityDataAccess cacheAccess, SessionImplementor session) {
122+
super( persistenceContextLookupStrategy, persister, cacheAccess, session );
123+
this.graphForDynamicEntity = session.getSessionFactory().createGraphForDynamicEntity( persister.getEntityName() );
124+
}
125+
126+
@Override
127+
protected Object lookupByIdentifier(EntityKey entityKey) {
128+
return session.find( graphForDynamicEntity, entityKey.getIdentifier() );
129+
}
130+
}
131+
132+
@SuppressForbiddenApis(reason = "EntityPersister is needed to retrieve/use EntityDataAccess")
133+
private static class PojoPersistenceContextThenSecondLevelCacheLookupStrategy
134+
extends PersistenceContextThenSecondLevelCacheLookupStrategy {
135+
136+
private PojoPersistenceContextThenSecondLevelCacheLookupStrategy(
137+
EntityLoadingCacheLookupStrategyImplementor persistenceContextLookupStrategy, EntityPersister persister,
138+
EntityDataAccess cacheAccess, SessionImplementor session) {
139+
super( persistenceContextLookupStrategy, persister, cacheAccess, session );
140+
}
141+
142+
@Override
143+
protected Object lookupByIdentifier(EntityKey entityKey) {
144+
return session.find( persister.getMappedClass(), entityKey.getIdentifier() );
145+
}
146+
}
95147
}

0 commit comments

Comments
 (0)