Skip to content

Commit 7407791

Browse files
committed
add IdentifierLoadAccess.withReadOnly()
adding this to the other XxxxLoadAccess interfaces is much harder, but this one is easy
1 parent e02e1e2 commit 7407791

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

hibernate-core/src/main/java/org/hibernate/IdentifierLoadAccess.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public interface IdentifierLoadAccess<T> {
3636
*/
3737
IdentifierLoadAccess<T> with(CacheMode cacheMode);
3838

39+
/**
40+
* Specify whether the entity should be loaded in read-only mode.
41+
*
42+
* @see Session#setDefaultReadOnly(boolean)
43+
*/
44+
IdentifierLoadAccess<T> withReadOnly(boolean readOnly);
45+
3946
default IdentifierLoadAccess<T> with(RootGraph<T> graph) {
4047
return with( graph, GraphSemantic.LOAD );
4148
}

hibernate-core/src/main/java/org/hibernate/LockOptions.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ public LockOptions setLockMode(LockMode lockMode) {
116116
* @param lockMode The lock mode to apply to the given alias
117117
* @return this LockRequest instance for operation chaining.
118118
*
119-
* @see Query#setLockMode(String, LockMode)
120-
* @see Criteria#setLockMode(LockMode)
121-
* @see Criteria#setLockMode(String, LockMode)
119+
* @see org.hibernate.query.Query#setLockMode(String, LockMode)
122120
*/
123121
public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode) {
124122
if ( aliasSpecificLockModes == null ) {

hibernate-core/src/main/java/org/hibernate/loader/access/IdentifierLoadAccessImpl.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T>, Jav
4040

4141
private LockOptions lockOptions;
4242
private CacheMode cacheMode;
43+
private Boolean readOnly;
4344
private RootGraphImplementor<T> rootGraph;
4445
private GraphSemantic graphSemantic;
4546

@@ -60,6 +61,12 @@ public IdentifierLoadAccess<T> with(CacheMode cacheMode) {
6061
return this;
6162
}
6263

64+
@Override
65+
public IdentifierLoadAccess<T> withReadOnly(boolean readOnly) {
66+
this.readOnly = readOnly;
67+
return this;
68+
}
69+
6370
@Override
6471
public IdentifierLoadAccess<T> with(RootGraph<T> graph, GraphSemantic semantic) {
6572
this.rootGraph = (RootGraphImplementor<T>) graph;
@@ -122,21 +129,21 @@ protected T doGetReference(Object id) {
122129
id = entityPersister.getIdentifierMapping().getJavaTypeDescriptor().coerce( id, this );
123130
}
124131

132+
String entityName = entityPersister.getEntityName();
133+
Boolean readOnly = this.readOnly != null ? this.readOnly : loadQueryInfluencers.getReadOnly();
134+
125135
if ( this.lockOptions != null ) {
126-
LoadEvent event = new LoadEvent( id, entityPersister.getEntityName(), lockOptions, eventSource, loadQueryInfluencers.getReadOnly() );
136+
LoadEvent event = new LoadEvent( id, entityName, lockOptions, eventSource, readOnly );
127137
context.fireLoad( event, LoadEventListener.LOAD );
128138
return (T) event.getResult();
129139
}
130140

131-
LoadEvent event = new LoadEvent( id, entityPersister.getEntityName(), false, eventSource, loadQueryInfluencers.getReadOnly() );
141+
LoadEvent event = new LoadEvent( id, entityName, false, eventSource, readOnly );
132142
boolean success = false;
133143
try {
134144
context.fireLoad( event, LoadEventListener.LOAD );
135145
if ( event.getResult() == null ) {
136-
session.getFactory().getEntityNotFoundDelegate().handleEntityNotFound(
137-
entityPersister.getEntityName(),
138-
id
139-
);
146+
session.getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id );
140147
}
141148
success = true;
142149
return (T) event.getResult();
@@ -167,16 +174,19 @@ protected final T doLoad(Object id) {
167174
id = entityPersister.getIdentifierMapping().getJavaTypeDescriptor().coerce( id, this );
168175
}
169176

177+
String entityName = entityPersister.getEntityName();
178+
Boolean readOnly = this.readOnly != null ? this.readOnly : loadQueryInfluencers.getReadOnly();
179+
170180
if ( this.lockOptions != null ) {
171-
LoadEvent event = new LoadEvent( id, entityPersister.getEntityName(), lockOptions, eventSource, loadQueryInfluencers.getReadOnly() );
181+
LoadEvent event = new LoadEvent( id, entityName, lockOptions, eventSource, readOnly );
172182
context.fireLoad( event, LoadEventListener.GET );
173183
final Object result = event.getResult();
174184
initializeIfNecessary( result );
175185

176186
return (T) result;
177187
}
178188

179-
LoadEvent event = new LoadEvent( id, entityPersister.getEntityName(), false, eventSource, loadQueryInfluencers.getReadOnly() );
189+
LoadEvent event = new LoadEvent( id, entityName, false, eventSource, readOnly );
180190
boolean success = false;
181191
try {
182192
context.fireLoad( event, LoadEventListener.GET );
@@ -206,17 +216,15 @@ private void initializeIfNecessary(Object result) {
206216
if ( initializer.isUninitialized() ) {
207217
initializer.initialize();
208218
}
209-
return;
210219
}
211-
212-
final BytecodeEnhancementMetadata enhancementMetadata = entityPersister.getEntityMetamodel().getBytecodeEnhancementMetadata();
213-
if ( ! enhancementMetadata.isEnhancedForLazyLoading() ) {
214-
return;
215-
}
216-
217-
final BytecodeLazyAttributeInterceptor interceptor = enhancementMetadata.extractLazyInterceptor( result);
218-
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
219-
( (EnhancementAsProxyLazinessInterceptor) interceptor ).forceInitialize( result, null );
220+
else {
221+
final BytecodeEnhancementMetadata enhancementMetadata = entityPersister.getEntityMetamodel().getBytecodeEnhancementMetadata();
222+
if ( enhancementMetadata.isEnhancedForLazyLoading() ) {
223+
final BytecodeLazyAttributeInterceptor interceptor = enhancementMetadata.extractLazyInterceptor( result);
224+
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
225+
( (EnhancementAsProxyLazinessInterceptor) interceptor ).forceInitialize( result, null );
226+
}
227+
}
220228
}
221229
}
222230

0 commit comments

Comments
 (0)