下面列出了org.hibernate.LockOptions#setLockMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void cascade(
EventSource session,
Object child,
String entityName,
Object anything,
boolean isCascadeDeleteEnabled) {
LOG.tracev( "Cascading to lock: {0}", entityName );
LockMode lockMode = LockMode.NONE;
LockOptions lr = new LockOptions();
if ( anything instanceof LockOptions ) {
LockOptions lockOptions = (LockOptions) anything;
lr.setTimeOut( lockOptions.getTimeOut() );
lr.setScope( lockOptions.getScope() );
lr.setFollowOnLocking( lockOptions.getFollowOnLocking() );
if ( lockOptions.getScope() ) {
lockMode = lockOptions.getLockMode();
}
}
lr.setLockMode( lockMode );
session.buildLockRequest( lr ).lock( entityName, child );
}
/**
* Get the <tt>FOR UPDATE OF column_list</tt> fragment appropriate for this
* dialect given the aliases of the columns to be write locked.
*
* @param aliases The columns to be write locked.
* @param lockOptions the lock options to apply
* @return The appropriate <tt>FOR UPDATE OF column_list</tt> clause string.
*/
@SuppressWarnings({"unchecked", "UnusedParameters"})
public String getForUpdateString(String aliases, LockOptions lockOptions) {
LockMode lockMode = lockOptions.getLockMode();
final Iterator<Map.Entry<String, LockMode>> itr = lockOptions.getAliasLockIterator();
while ( itr.hasNext() ) {
// seek the highest lock mode
final Map.Entry<String, LockMode>entry = itr.next();
final LockMode lm = entry.getValue();
if ( lm.greaterThan( lockMode ) ) {
lockMode = lm;
}
}
lockOptions.setLockMode( lockMode );
return getForUpdateString( lockOptions );
}
@Override
public LockOptions getLockRequest(LockModeType lockModeType, Map<String, Object> properties) {
LockOptions lockOptions = new LockOptions();
LockOptions.copy( this.lockOptions, lockOptions );
lockOptions.setLockMode( LockModeTypeHelper.getLockMode( lockModeType ) );
if ( properties != null ) {
setLockOptions( properties, lockOptions );
}
return lockOptions;
}
@Override
public String getForUpdateString(final String aliases, final LockOptions lockOptions) {
LockMode lockMode = lockOptions.findGreatestLockMode();
lockOptions.setLockMode( lockMode );
// not sure why this is sometimes empty
if ( aliases == null || aliases.isEmpty() ) {
return getForUpdateString( lockOptions );
}
return getForUpdateString( aliases, lockMode, lockOptions.getTimeOut() );
}
private LoadEvent(
Serializable entityId,
String entityClassName,
Object instanceToLoad,
LockOptions lockOptions,
boolean isAssociationFetch,
EventSource source) {
super(source);
if ( entityId == null ) {
throw new IllegalArgumentException("id to load is required for loading");
}
if ( lockOptions.getLockMode() == LockMode.WRITE ) {
throw new IllegalArgumentException("Invalid lock mode for loading");
}
else if ( lockOptions.getLockMode() == null ) {
lockOptions.setLockMode(DEFAULT_LOCK_MODE);
}
this.entityId = entityId;
this.entityClassName = entityClassName;
this.instanceToLoad = instanceToLoad;
this.lockOptions = lockOptions;
this.isAssociationFetch = isAssociationFetch;
this.postLoadEvent = new PostLoadEvent( source );
}
public ResolveNaturalIdEvent(
Map<String, Object> naturalIdValues,
EntityPersister entityPersister,
LockOptions lockOptions,
EventSource source) {
super( source );
if ( entityPersister == null ) {
throw new IllegalArgumentException( "EntityPersister is required for loading" );
}
if ( ! entityPersister.hasNaturalIdentifier() ) {
throw new HibernateException( "Entity did not define a natural-id" );
}
if ( naturalIdValues == null || naturalIdValues.isEmpty() ) {
throw new IllegalArgumentException( "natural-id to load is required" );
}
if ( entityPersister.getNaturalIdentifierProperties().length != naturalIdValues.size() ) {
throw new HibernateException(
String.format(
"Entity [%s] defines its natural-id with %d properties but only %d were specified",
entityPersister.getEntityName(),
entityPersister.getNaturalIdentifierProperties().length,
naturalIdValues.size()
)
);
}
if ( lockOptions.getLockMode() == LockMode.WRITE ) {
throw new IllegalArgumentException( "Invalid lock mode for loading" );
}
else if ( lockOptions.getLockMode() == null ) {
lockOptions.setLockMode( DEFAULT_LOCK_MODE );
}
this.entityPersister = entityPersister;
this.naturalIdValues = naturalIdValues;
this.lockOptions = lockOptions;
int[] naturalIdPropertyPositions = entityPersister.getNaturalIdentifierProperties();
orderedNaturalIdValues = new Object[naturalIdPropertyPositions.length];
int i = 0;
for ( int position : naturalIdPropertyPositions ) {
final String propertyName = entityPersister.getPropertyNames()[position];
if ( ! naturalIdValues.containsKey( propertyName ) ) {
throw new HibernateException(
String.format( "No value specified for natural-id property %s#%s", getEntityName(), propertyName )
);
}
orderedNaturalIdValues[i++] = naturalIdValues.get( entityPersister.getPropertyNames()[position] );
}
}