4
4
*/
5
5
package org .hibernate .search .mapper .orm .loading .impl ;
6
6
7
+ import java .util .Map ;
8
+
7
9
import org .hibernate .ObjectNotFoundException ;
8
10
import org .hibernate .cache .spi .access .EntityDataAccess ;
9
11
import org .hibernate .engine .spi .EntityKey ;
10
12
import org .hibernate .engine .spi .SessionImplementor ;
13
+ import org .hibernate .graph .RootGraph ;
14
+ import org .hibernate .metamodel .RepresentationMode ;
11
15
import org .hibernate .metamodel .mapping .EntityMappingType ;
12
16
import org .hibernate .persister .entity .EntityPersister ;
13
17
import org .hibernate .search .mapper .orm .logging .impl .OrmMiscLog ;
22
26
* @author Emmanuel Bernard
23
27
*/
24
28
@ SuppressForbiddenApis (reason = "EntityPersister is needed to retrieve/use EntityDataAccess" )
25
- class PersistenceContextThenSecondLevelCacheLookupStrategy
29
+ abstract class PersistenceContextThenSecondLevelCacheLookupStrategy
26
30
implements EntityLoadingCacheLookupStrategyImplementor {
27
31
28
32
static EntityLoadingCacheLookupStrategyImplementor create (EntityMappingType entityMappingType ,
@@ -37,18 +41,29 @@ static EntityLoadingCacheLookupStrategyImplementor create(EntityMappingType enti
37
41
entityPersister .getEntityName () );
38
42
return persistenceContextLookupStrategy ;
39
43
}
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
+ }
46
61
}
47
62
48
63
private final EntityLoadingCacheLookupStrategyImplementor persistenceContextLookupStrategy ;
49
- private final EntityPersister persister ;
50
64
private final EntityDataAccess cacheAccess ;
51
- private final SessionImplementor session ;
65
+ protected final EntityPersister persister ;
66
+ protected final SessionImplementor session ;
52
67
53
68
private PersistenceContextThenSecondLevelCacheLookupStrategy (
54
69
EntityLoadingCacheLookupStrategyImplementor persistenceContextLookupStrategy ,
@@ -84,12 +99,49 @@ public Object lookup(EntityKey entityKey) {
84
99
85
100
try {
86
101
// This will load the object from the second level cache
87
- return session . byId ( persister . getEntityName () ). load ( entityKey . getIdentifier () );
102
+ return lookupByIdentifier ( entityKey );
88
103
}
89
104
catch (ObjectNotFoundException ignored ) {
90
105
// Unlikely but needed: an index might be out of sync, and the cache might be as well
91
106
// Ignore the exception and handle as a cache miss by returning null
92
107
return null ;
93
108
}
94
109
}
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
+ }
95
147
}
0 commit comments