Java Code Examples for org.hibernate.engine.spi.PersistenceContext#initializeNonLazyCollections()
The following examples show how to use
org.hibernate.engine.spi.PersistenceContext#initializeNonLazyCollections() .
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 |
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 |
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 |
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 |
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 ); } }
Example 5
Source File: DefaultLoadEventListener.java From lams with GNU General Public License v2.0 | 4 votes |
private Object convertCacheEntryToEntity( CacheEntry entry, Serializable entityId, EntityPersister persister, LoadEvent event, EntityKey entityKey) { final EventSource session = event.getSession(); final SessionFactoryImplementor factory = session.getFactory(); final EntityPersister subclassPersister; if ( traceEnabled ) { LOG.tracef( "Converting second-level cache entry [%s] into entity : %s", entry, MessageHelper.infoString( persister, entityId, factory ) ); } final Object entity; subclassPersister = factory.getEntityPersister( entry.getSubclass() ); final Object optionalObject = event.getInstanceToLoad(); entity = optionalObject == null ? session.instantiate( subclassPersister, entityId ) : optionalObject; // make it circular-reference safe TwoPhaseLoad.addUninitializedCachedEntity( entityKey, entity, subclassPersister, LockMode.NONE, entry.getVersion(), session ); final PersistenceContext persistenceContext = session.getPersistenceContext(); final Object[] values; final Object version; final boolean isReadOnly; final Type[] types = subclassPersister.getPropertyTypes(); // initializes the entity by (desired) side-effect values = ( (StandardCacheEntryImpl) entry ).assemble( entity, entityId, subclassPersister, session.getInterceptor(), session ); if ( ( (StandardCacheEntryImpl) entry ).isDeepCopyNeeded() ) { TypeHelper.deepCopy( values, types, subclassPersister.getPropertyUpdateability(), values, session ); } version = Versioning.getVersion( values, subclassPersister ); LOG.tracef( "Cached Version : %s", version ); final Object proxy = persistenceContext.getProxy( entityKey ); if ( proxy != null ) { // there is already a proxy for this impl // only set the status to read-only if the proxy is read-only isReadOnly = ( (HibernateProxy) proxy ).getHibernateLazyInitializer().isReadOnly(); } else { isReadOnly = session.isDefaultReadOnly(); } persistenceContext.addEntry( entity, ( isReadOnly ? Status.READ_ONLY : Status.MANAGED ), values, null, entityId, version, LockMode.NONE, true, subclassPersister, false ); subclassPersister.afterInitialize( entity, session ); persistenceContext.initializeNonLazyCollections(); //PostLoad is needed for EJB3 PostLoadEvent postLoadEvent = event.getPostLoadEvent() .setEntity( entity ) .setId( entityId ) .setPersister( persister ); for ( PostLoadEventListener listener : postLoadEventListeners( session ) ) { listener.onPostLoad( postLoadEvent ); } return entity; }