Java Code Examples for org.hibernate.LockMode#READ

The following examples show how to use org.hibernate.LockMode#READ . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Convert from the Hibernate specific LockMode to the JPA defined LockModeType.
 *
 * @param lockMode The Hibernate LockMode.
 *
 * @return The JPA LockModeType
 */
public static LockModeType convertToLockModeType(LockMode lockMode) {
	if ( lockMode == LockMode.NONE ) {
		return LockModeType.NONE;
	}
	else if ( lockMode == LockMode.OPTIMISTIC || lockMode == LockMode.READ ) {
		return LockModeType.OPTIMISTIC;
	}
	else if ( lockMode == LockMode.OPTIMISTIC_FORCE_INCREMENT || lockMode == LockMode.WRITE ) {
		return LockModeType.OPTIMISTIC_FORCE_INCREMENT;
	}
	else if ( lockMode == LockMode.PESSIMISTIC_READ ) {
		return LockModeType.PESSIMISTIC_READ;
	}
	else if ( lockMode == LockMode.PESSIMISTIC_WRITE
			|| lockMode == LockMode.UPGRADE
			|| lockMode == LockMode.UPGRADE_NOWAIT
			|| lockMode == LockMode.UPGRADE_SKIPLOCKED) {
		return LockModeType.PESSIMISTIC_WRITE;
	}
	else if ( lockMode == LockMode.PESSIMISTIC_FORCE_INCREMENT
			|| lockMode == LockMode.FORCE ) {
		return LockModeType.PESSIMISTIC_FORCE_INCREMENT;
	}
	throw new AssertionFailure( "unhandled lock mode " + lockMode );
}
 
Example 2
private static LockMode getLockMode(String lockMode) {
	if ( lockMode == null || "read".equals( lockMode ) ) {
		return LockMode.READ;
	}
	else if ( "none".equals( lockMode ) ) {
		return LockMode.NONE;
	}
	else if ( "upgrade".equals( lockMode ) ) {
		return LockMode.UPGRADE;
	}
	else if ( "upgrade-nowait".equals( lockMode ) ) {
		return LockMode.UPGRADE_NOWAIT;
	}
	else if ( "write".equals( lockMode ) ) {
		return LockMode.WRITE;
	}
	else if ( "force".equals( lockMode ) ) {
		return LockMode.FORCE;
	}
	else {
		throw new MappingException( "unknown lockmode" );
	}
}
 
Example 3
protected final UniqueEntityLoader getLoaderByLockMode(LockMode lockMode) {
	if ( LockMode.NONE == lockMode ) {
		return noneLockLoader;
	}
	else if ( LockMode.READ == lockMode ) {
		return readLockLoader;
	}

	return loaders.computeIfAbsent( lockMode, this::generateDelayedEntityLoader );
}
 
Example 4
Source Project: lams   File: QueryBinder.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void bindNativeQuery(
		org.hibernate.annotations.NamedNativeQuery queryAnn,
		MetadataBuildingContext context) {
	if ( queryAnn == null ) {
		return;
	}

	//ResultSetMappingDefinition mappingDefinition = mappings.getResultSetMapping( queryAnn.resultSetMapping() );
	if ( BinderHelper.isEmptyAnnotationValue( queryAnn.name() ) ) {
		throw new AnnotationException( "A named query must have a name when used in class or package level" );
	}

	NamedSQLQueryDefinition query;
	String resultSetMapping = queryAnn.resultSetMapping();
	if ( !BinderHelper.isEmptyAnnotationValue( resultSetMapping ) ) {
		//sql result set usage
		query = new NamedSQLQueryDefinitionBuilder().setName( queryAnn.name() )
				.setQuery( queryAnn.query() )
				.setResultSetRef( resultSetMapping )
				.setQuerySpaces( null )
				.setCacheable( queryAnn.cacheable() )
				.setCacheRegion(
						BinderHelper.isEmptyAnnotationValue( queryAnn.cacheRegion() ) ?
								null :
								queryAnn.cacheRegion()
				)
				.setTimeout( queryAnn.timeout() < 0 ? null : queryAnn.timeout() )
				.setFetchSize( queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize() )
				.setFlushMode( getFlushMode( queryAnn.flushMode() ) )
				.setCacheMode( getCacheMode( queryAnn.cacheMode() ) )
				.setReadOnly( queryAnn.readOnly() )
				.setComment( BinderHelper.isEmptyAnnotationValue( queryAnn.comment() ) ? null : queryAnn.comment() )
				.setParameterTypes( null )
				.setCallable( queryAnn.callable() )
				.createNamedQueryDefinition();
	}
	else if ( !void.class.equals( queryAnn.resultClass() ) ) {
		//class mapping usage
		//FIXME should be done in a second pass due to entity name?
		final NativeSQLQueryRootReturn entityQueryReturn =
				new NativeSQLQueryRootReturn( "alias1", queryAnn.resultClass().getName(), new HashMap(), LockMode.READ );
		query = new NamedSQLQueryDefinitionBuilder().setName( queryAnn.name() )
				.setQuery( queryAnn.query() )
				.setQueryReturns( new NativeSQLQueryReturn[] {entityQueryReturn} )
				.setQuerySpaces( null )
				.setCacheable( queryAnn.cacheable() )
				.setCacheRegion(
						BinderHelper.isEmptyAnnotationValue( queryAnn.cacheRegion() ) ?
								null :
								queryAnn.cacheRegion()
				)
				.setTimeout( queryAnn.timeout() < 0 ? null : queryAnn.timeout() )
				.setFetchSize( queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize() )
				.setFlushMode( getFlushMode( queryAnn.flushMode() ) )
				.setCacheMode( getCacheMode( queryAnn.cacheMode() ) )
				.setReadOnly( queryAnn.readOnly() )
				.setComment( BinderHelper.isEmptyAnnotationValue( queryAnn.comment() ) ? null : queryAnn.comment() )
				.setParameterTypes( null )
				.setCallable( queryAnn.callable() )
				.createNamedQueryDefinition();
	}
	else {
		throw new NotYetImplementedException( "Pure native scalar queries are not yet supported" );
	}
	context.getMetadataCollector().addNamedNativeQuery( query );
	if ( LOG.isDebugEnabled() ) {
		LOG.debugf( "Binding named native query: %s => %s", query.getName(), queryAnn.query() );
	}
}
 
Example 5
Source Project: lams   File: Loader.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * The entity instance is not in the session cache
 */
private Object instanceNotYetLoaded(
		final ResultSet rs,
		final int i,
		final Loadable persister,
		final String rowIdAlias,
		final EntityKey key,
		final LockMode lockMode,
		final EntityKey optionalObjectKey,
		final Object optionalObject,
		final List hydratedObjects,
		final SharedSessionContractImplementor session)
		throws HibernateException, SQLException {
	final String instanceClass = getInstanceClass(
			rs,
			i,
			persister,
			key.getIdentifier(),
			session
	);

	// see if the entity defines reference caching, and if so use the cached reference (if one).
	if ( session.getCacheMode().isGetEnabled() && persister.canUseReferenceCacheEntries() ) {
		final EntityDataAccess cache = persister.getCacheAccessStrategy();
		final Object ck = cache.generateCacheKey(
				key.getIdentifier(),
				persister,
				session.getFactory(),
				session.getTenantIdentifier()
				);
		final Object cachedEntry = CacheHelper.fromSharedCache( session, ck, cache );
		if ( cachedEntry != null ) {
			CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure().destructure( cachedEntry, factory );
			return ( (ReferenceCacheEntryImpl) entry ).getReference();
		}
	}

	final Object object;
	if ( optionalObjectKey != null && key.equals( optionalObjectKey ) ) {
		//its the given optional object
		object = optionalObject;
	}
	else {
		// instantiate a new instance
		object = session.instantiate( instanceClass, key.getIdentifier() );
	}

	//need to hydrate it.

	// grab its state from the ResultSet and keep it in the Session
	// (but don't yet initialize the object itself)
	// note that we acquire LockMode.READ even if it was not requested
	LockMode acquiredLockMode = lockMode == LockMode.NONE ? LockMode.READ : lockMode;
	loadFromResultSet(
			rs,
			i,
			object,
			instanceClass,
			key,
			rowIdAlias,
			acquiredLockMode,
			persister,
			session
	);

	//materialize associations (and initialize the object) later
	hydratedObjects.add( object );

	return object;
}
 
Example 6
@Override
public void hydrateEntityState(ResultSet resultSet, ResultSetProcessingContextImpl context) {
	final EntityReferenceProcessingState processingState = context.getProcessingState( entityReference );

	// If there is no identifier for this entity reference for this row, nothing to do
	if ( processingState.isMissingIdentifier() ) {
		handleMissingIdentifier( context );
		return;
	}

	// make sure we have the EntityKey
	final EntityKey entityKey = processingState.getEntityKey();
	if ( entityKey == null ) {
		handleMissingIdentifier( context );
		return;
	}

	// Have we already hydrated this entity's state?
	if ( processingState.getEntityInstance() != null ) {
		return;
	}


	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// In getting here, we know that:
	// 		1) We need to hydrate the entity state
	//		2) We have a valid EntityKey for the entity

	// see if we have an existing entry in the session for this EntityKey
	final Object existing = context.getSession().getEntityUsingInterceptor( entityKey );
	if ( existing != null ) {
		// It is previously associated with the Session, perform some checks
		if ( ! entityReference.getEntityPersister().isInstance( existing ) ) {
			throw new WrongClassException(
					"loaded object was of wrong class " + existing.getClass(),
					entityKey.getIdentifier(),
					entityReference.getEntityPersister().getEntityName()
			);
		}
		checkVersion( resultSet, context, entityKey, existing );

		// use the existing association as the hydrated state
		processingState.registerEntityInstance( existing );
		//context.registerHydratedEntity( entityReference, entityKey, existing );
		return;
	}

	// Otherwise, we need to load it from the ResultSet...

	// determine which entity instance to use.  Either the supplied one, or instantiate one
	Object entityInstance = null;
	if ( isReturn &&
			context.shouldUseOptionalEntityInformation() &&
			context.getQueryParameters().getOptionalObject() != null ) {
		final EntityKey optionalEntityKey = ResultSetProcessorHelper.getOptionalObjectKey(
				context.getQueryParameters(),
				context.getSession()
		);
		if ( optionalEntityKey != null && optionalEntityKey.equals( entityKey ) ) {
			entityInstance = context.getQueryParameters().getOptionalObject();
		}
	}

	final String concreteEntityTypeName = getConcreteEntityTypeName( resultSet, context, entityKey );
	if ( entityInstance == null ) {
		entityInstance = context.getSession().instantiate( concreteEntityTypeName, entityKey.getIdentifier() );
	}
	processingState.registerEntityInstance( entityInstance );

	// need to hydrate it.
	// grab its state from the ResultSet and keep it in the Session
	// (but don't yet initialize the object itself)
	// note that we acquire LockMode.READ even if it was not requested
	log.trace( "hydrating entity state" );
	final LockMode requestedLockMode = context.resolveLockMode( entityReference );
	final LockMode lockModeToAcquire = requestedLockMode == LockMode.NONE
			? LockMode.READ
			: requestedLockMode;

	loadFromResultSet(
			resultSet,
			context,
			entityInstance,
			concreteEntityTypeName,
			entityKey,
			lockModeToAcquire
	);
}
 
Example 7
/**
 * The entity instance is not in the session cache
 */
private Object instanceNotYetLoaded(
        final ResultSet rs,
        final int i,
        final Loadable persister,
        final String rowIdAlias,
        final EntityKey key,
        final LockMode lockMode,
        final EntityKey optionalObjectKey,
        final Object optionalObject,
        final List hydratedObjects,
        final SessionImplementor session) 
throws HibernateException, SQLException {

	final String instanceClass = getInstanceClass( 
			rs, 
			i, 
			persister, 
			key.getIdentifier(), 
			session 
		);

	final Object object;
	if ( optionalObjectKey != null && key.equals( optionalObjectKey ) ) {
		//its the given optional object
		object = optionalObject;
	}
	else {
		// instantiate a new instance
		object = session.instantiate( instanceClass, key.getIdentifier() );
	}

	//need to hydrate it.

	// grab its state from the ResultSet and keep it in the Session
	// (but don't yet initialize the object itself)
	// note that we acquire LockMode.READ even if it was not requested
	LockMode acquiredLockMode = lockMode == LockMode.NONE ? LockMode.READ : lockMode;
	loadFromResultSet( 
			rs, 
			i, 
			object, 
			instanceClass, 
			key, 
			rowIdAlias, 
			acquiredLockMode, 
			persister, 
			session 
		);

	//materialize associations (and initialize the object) later
	hydratedObjects.add( object );

	return object;
}