Java Code Examples for org.hibernate.engine.spi.PersistenceContext#afterLoad()

The following examples show how to use org.hibernate.engine.spi.PersistenceContext#afterLoad() . 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
public List doQueryAndInitializeNonLazyCollections(
		final SharedSessionContractImplementor session,
		final QueryParameters queryParameters,
		final boolean returnProxies,
		final ResultTransformer forcedResultTransformer)
		throws HibernateException, SQLException {
	final PersistenceContext persistenceContext = session.getPersistenceContext();
	boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
	if ( queryParameters.isReadOnlyInitialized() ) {
		// The read-only/modifiable mode for the query was explicitly set.
		// Temporarily set the default read-only/modifiable setting to the query's setting.
		persistenceContext.setDefaultReadOnly( queryParameters.isReadOnly() );
	}
	else {
		// The read-only/modifiable setting for the query was not initialized.
		// Use the default read-only/modifiable from the persistence context instead.
		queryParameters.setReadOnly( persistenceContext.isDefaultReadOnly() );
	}
	persistenceContext.beforeLoad();
	List result;
	try {
		try {
			result = doQuery( session, queryParameters, returnProxies, forcedResultTransformer );
		}
		finally {
			persistenceContext.afterLoad();
		}
		persistenceContext.initializeNonLazyCollections();
	}
	finally {
		// Restore the original default
		persistenceContext.setDefaultReadOnly( defaultReadOnlyOrig );
	}
	return result;
}
 
Example 2
Source File: DynamicBatchingEntityLoaderBuilder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public List doEntityBatchFetch(
		SharedSessionContractImplementor session,
		QueryParameters queryParameters,
		Serializable[] ids) {
	final String sql = StringHelper.expandBatchIdPlaceholder(
			sqlTemplate,
			ids,
			alias,
			persister.getKeyColumnNames(),
			session.getJdbcServices().getJdbcEnvironment().getDialect()
	);

	try {
		final PersistenceContext persistenceContext = session.getPersistenceContext();
		boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
		if ( queryParameters.isReadOnlyInitialized() ) {
			// The read-only/modifiable mode for the query was explicitly set.
			// Temporarily set the default read-only/modifiable setting to the query's setting.
			persistenceContext.setDefaultReadOnly( queryParameters.isReadOnly() );
		}
		else {
			// The read-only/modifiable setting for the query was not initialized.
			// Use the default read-only/modifiable from the persistence context instead.
			queryParameters.setReadOnly( persistenceContext.isDefaultReadOnly() );
		}
		persistenceContext.beforeLoad();
		List results;
		try {
			try {
				results = doTheLoad( sql, queryParameters, session );
			}
			finally {
				persistenceContext.afterLoad();
			}
			persistenceContext.initializeNonLazyCollections();
			log.debug( "Done batch load" );
			return results;
		}
		finally {
			// Restore the original default
			persistenceContext.setDefaultReadOnly( defaultReadOnlyOrig );
		}
	}
	catch ( SQLException sqle ) {
		throw session.getJdbcServices().getSqlExceptionHelper().convert(
				sqle,
				"could not load an entity batch: " + MessageHelper.infoString(
						getEntityPersisters()[0],
						ids,
						session.getFactory()
				),
				sql
		);
	}
}
 
Example 3
Source File: DynamicBatchingCollectionInitializerBuilder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public final void doBatchedCollectionLoad(
		final SharedSessionContractImplementor session,
		final Serializable[] ids,
		final Type type) throws HibernateException {

	if ( LOG.isDebugEnabled() ) {
		LOG.debugf(
				"Batch loading collection: %s",
				MessageHelper.collectionInfoString( getCollectionPersisters()[0], ids, getFactory() )
		);
	}

	final Type[] idTypes = new Type[ids.length];
	Arrays.fill( idTypes, type );
	final QueryParameters queryParameters = new QueryParameters( idTypes, ids, ids );

	final String sql = StringHelper.expandBatchIdPlaceholder(
			sqlTemplate,
			ids,
			alias,
			collectionPersister().getKeyColumnNames(),
			session.getJdbcServices().getJdbcEnvironment().getDialect()
	);

	try {
		final PersistenceContext persistenceContext = session.getPersistenceContext();
		boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
		if ( queryParameters.isReadOnlyInitialized() ) {
			// The read-only/modifiable mode for the query was explicitly set.
			// Temporarily set the default read-only/modifiable setting to the query's setting.
			persistenceContext.setDefaultReadOnly( queryParameters.isReadOnly() );
		}
		else {
			// The read-only/modifiable setting for the query was not initialized.
			// Use the default read-only/modifiable from the persistence context instead.
			queryParameters.setReadOnly( persistenceContext.isDefaultReadOnly() );
		}
		persistenceContext.beforeLoad();
		try {
			try {
				doTheLoad( sql, queryParameters, session );
			}
			finally {
				persistenceContext.afterLoad();
			}
			persistenceContext.initializeNonLazyCollections();
		}
		finally {
			// Restore the original default
			persistenceContext.setDefaultReadOnly( defaultReadOnlyOrig );
		}
	}
	catch ( SQLException e ) {
		throw session.getJdbcServices().getSqlExceptionHelper().convert(
				e,
				"could not initialize a collection batch: " +
						MessageHelper.collectionInfoString( collectionPersister(), ids, getFactory() ),
				sql
		);
	}

	LOG.debug( "Done batch load" );

}
 
Example 4
Source File: AbstractLoadPlanBasedLoader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected List executeLoad(
		SharedSessionContractImplementor session,
		QueryParameters queryParameters,
		LoadQueryDetails loadQueryDetails,
		boolean returnProxies,
		ResultTransformer forcedResultTransformer,
		List<AfterLoadAction> afterLoadActions) throws SQLException {
	final PersistenceContext persistenceContext = session.getPersistenceContext();
	final boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
	if ( queryParameters.isReadOnlyInitialized() ) {
		// The read-only/modifiable mode for the query was explicitly set.
		// Temporarily set the default read-only/modifiable setting to the query's setting.
		persistenceContext.setDefaultReadOnly( queryParameters.isReadOnly() );
	}
	else {
		// The read-only/modifiable setting for the query was not initialized.
		// Use the default read-only/modifiable from the persistence context instead.
		queryParameters.setReadOnly( persistenceContext.isDefaultReadOnly() );
	}
	persistenceContext.beforeLoad();
	try {
		List results = null;
		final String sql = loadQueryDetails.getSqlStatement();
		SqlStatementWrapper wrapper = null;
		try {
			wrapper = executeQueryStatement( sql, queryParameters, false, afterLoadActions, session );
			results = loadQueryDetails.getResultSetProcessor().extractResults(
					wrapper.getResultSet(),
					session,
					queryParameters,
					new NamedParameterContext() {
						@Override
						public int[] getNamedParameterLocations(String name) {
							return AbstractLoadPlanBasedLoader.this.getNamedParameterLocs( name );
						}
					},
					returnProxies,
					queryParameters.isReadOnly(),
					forcedResultTransformer,
					afterLoadActions
			);
		}
		finally {
			if ( wrapper != null ) {
				session.getJdbcCoordinator().getResourceRegistry().release(
						wrapper.getResultSet(),
						wrapper.getStatement()
				);
				session.getJdbcCoordinator().getResourceRegistry().release( wrapper.getStatement() );
				session.getJdbcCoordinator().afterStatementExecution();
			}
			persistenceContext.afterLoad();
		}
		persistenceContext.initializeNonLazyCollections();
		return results;
	}
	finally {
		// Restore the original default
		persistenceContext.setDefaultReadOnly( defaultReadOnlyOrig );
	}
}