add IdentifierLoadAccess.withReadOnly()

adding this to the other XxxxLoadAccess interfaces is much harder,
but this one is easy
This commit is contained in:
Gavin King 2022-01-08 13:23:31 +01:00
parent e02e1e2ffb
commit 740779165d
3 changed files with 34 additions and 21 deletions

View File

@ -36,6 +36,13 @@ public interface IdentifierLoadAccess<T> {
*/
IdentifierLoadAccess<T> with(CacheMode cacheMode);
/**
* Specify whether the entity should be loaded in read-only mode.
*
* @see Session#setDefaultReadOnly(boolean)
*/
IdentifierLoadAccess<T> withReadOnly(boolean readOnly);
default IdentifierLoadAccess<T> with(RootGraph<T> graph) {
return with( graph, GraphSemantic.LOAD );
}

View File

@ -116,9 +116,7 @@ public class LockOptions implements Serializable {
* @param lockMode The lock mode to apply to the given alias
* @return this LockRequest instance for operation chaining.
*
* @see Query#setLockMode(String, LockMode)
* @see Criteria#setLockMode(LockMode)
* @see Criteria#setLockMode(String, LockMode)
* @see org.hibernate.query.Query#setLockMode(String, LockMode)
*/
public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode) {
if ( aliasSpecificLockModes == null ) {

View File

@ -40,6 +40,7 @@ public class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T>, Jav
private LockOptions lockOptions;
private CacheMode cacheMode;
private Boolean readOnly;
private RootGraphImplementor<T> rootGraph;
private GraphSemantic graphSemantic;
@ -60,6 +61,12 @@ public class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T>, Jav
return this;
}
@Override
public IdentifierLoadAccess<T> withReadOnly(boolean readOnly) {
this.readOnly = readOnly;
return this;
}
@Override
public IdentifierLoadAccess<T> with(RootGraph<T> graph, GraphSemantic semantic) {
this.rootGraph = (RootGraphImplementor<T>) graph;
@ -122,21 +129,21 @@ public class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T>, Jav
id = entityPersister.getIdentifierMapping().getJavaTypeDescriptor().coerce( id, this );
}
String entityName = entityPersister.getEntityName();
Boolean readOnly = this.readOnly != null ? this.readOnly : loadQueryInfluencers.getReadOnly();
if ( this.lockOptions != null ) {
LoadEvent event = new LoadEvent( id, entityPersister.getEntityName(), lockOptions, eventSource, loadQueryInfluencers.getReadOnly() );
LoadEvent event = new LoadEvent( id, entityName, lockOptions, eventSource, readOnly );
context.fireLoad( event, LoadEventListener.LOAD );
return (T) event.getResult();
}
LoadEvent event = new LoadEvent( id, entityPersister.getEntityName(), false, eventSource, loadQueryInfluencers.getReadOnly() );
LoadEvent event = new LoadEvent( id, entityName, false, eventSource, readOnly );
boolean success = false;
try {
context.fireLoad( event, LoadEventListener.LOAD );
if ( event.getResult() == null ) {
session.getFactory().getEntityNotFoundDelegate().handleEntityNotFound(
entityPersister.getEntityName(),
id
);
session.getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id );
}
success = true;
return (T) event.getResult();
@ -167,8 +174,11 @@ public class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T>, Jav
id = entityPersister.getIdentifierMapping().getJavaTypeDescriptor().coerce( id, this );
}
String entityName = entityPersister.getEntityName();
Boolean readOnly = this.readOnly != null ? this.readOnly : loadQueryInfluencers.getReadOnly();
if ( this.lockOptions != null ) {
LoadEvent event = new LoadEvent( id, entityPersister.getEntityName(), lockOptions, eventSource, loadQueryInfluencers.getReadOnly() );
LoadEvent event = new LoadEvent( id, entityName, lockOptions, eventSource, readOnly );
context.fireLoad( event, LoadEventListener.GET );
final Object result = event.getResult();
initializeIfNecessary( result );
@ -176,7 +186,7 @@ public class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T>, Jav
return (T) result;
}
LoadEvent event = new LoadEvent( id, entityPersister.getEntityName(), false, eventSource, loadQueryInfluencers.getReadOnly() );
LoadEvent event = new LoadEvent( id, entityName, false, eventSource, readOnly );
boolean success = false;
try {
context.fireLoad( event, LoadEventListener.GET );
@ -206,17 +216,15 @@ public class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T>, Jav
if ( initializer.isUninitialized() ) {
initializer.initialize();
}
return;
}
final BytecodeEnhancementMetadata enhancementMetadata = entityPersister.getEntityMetamodel().getBytecodeEnhancementMetadata();
if ( ! enhancementMetadata.isEnhancedForLazyLoading() ) {
return;
}
final BytecodeLazyAttributeInterceptor interceptor = enhancementMetadata.extractLazyInterceptor( result);
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
( (EnhancementAsProxyLazinessInterceptor) interceptor ).forceInitialize( result, null );
else {
final BytecodeEnhancementMetadata enhancementMetadata = entityPersister.getEntityMetamodel().getBytecodeEnhancementMetadata();
if ( enhancementMetadata.isEnhancedForLazyLoading() ) {
final BytecodeLazyAttributeInterceptor interceptor = enhancementMetadata.extractLazyInterceptor( result);
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
( (EnhancementAsProxyLazinessInterceptor) interceptor ).forceInitialize( result, null );
}
}
}
}