Java Code Examples for org.hibernate.engine.spi.EntityKey#equals()

The following examples show how to use org.hibernate.engine.spi.EntityKey#equals() . 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
Source File: Loader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean isCurrentRowForSameEntity(
		final EntityKey keyToRead,
		final int persisterIndex,
		final ResultSet resultSet,
		final SharedSessionContractImplementor session) throws SQLException {
	EntityKey currentRowKey = getKeyFromResultSet(
			persisterIndex, getEntityPersisters()[persisterIndex], null, resultSet, session
	);
	return keyToRead.equals( currentRowKey );
}
 
Example 2
Source File: Loader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private Object sequentialLoad(
		final ResultSet resultSet,
		final SharedSessionContractImplementor session,
		final QueryParameters queryParameters,
		final boolean returnProxies,
		final EntityKey keyToRead) throws HibernateException {

	final int entitySpan = getEntityPersisters().length;
	final List hydratedObjects = entitySpan == 0 ?
			null : new ArrayList( entitySpan );

	Object result = null;
	final EntityKey[] loadedKeys = new EntityKey[entitySpan];

	try {
		do {
			Object loaded = getRowFromResultSet(
					resultSet,
					session,
					queryParameters,
					getLockModes( queryParameters.getLockOptions() ),
					null,
					hydratedObjects,
					loadedKeys,
					returnProxies
			);
			if ( !keyToRead.equals( loadedKeys[0] ) ) {
				throw new AssertionFailure(
						String.format(
								"Unexpected key read for row; expected [%s]; actual [%s]",
								keyToRead,
								loadedKeys[0]
						)
				);
			}
			if ( result == null ) {
				result = loaded;
			}
		}
		while ( resultSet.next() &&
				isCurrentRowForSameEntity( keyToRead, 0, resultSet, session ) );
	}
	catch (SQLException sqle) {
		throw factory.getJdbcServices().getSqlExceptionHelper().convert(
				sqle,
				"could not doAfterTransactionCompletion sequential read of results (forward)",
				getSQLString()
		);
	}

	initializeEntitiesAndCollections(
			hydratedObjects,
			resultSet,
			session,
			queryParameters.isReadOnly( session )
	);
	session.getPersistenceContext().initializeNonLazyCollections();
	return result;
}
 
Example 3
Source File: Loader.java    From lams with 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 4
Source File: EntityReferenceInitializerImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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
	);
}