Package org.hibernate.engine.spi

Examples of org.hibernate.engine.spi.SessionFactoryImplementor


      //we have a detached collection thats set to null, reattach
      if ( sessionFactoryUuid == null ) {
        throw new LazyInitializationException( "could not initialize proxy - no Session" );
      }
      try {
        SessionFactoryImplementor sf = (SessionFactoryImplementor)
            SessionFactoryRegistry.INSTANCE.getSessionFactory( sessionFactoryUuid );
        SessionImplementor session = (SessionImplementor) sf.openSession();
       
        // TODO: On the next major release, add an
        // 'isJTA' or 'getTransactionFactory' method to Session.
        boolean isJTA = session.getTransactionCoordinator()
            .getTransactionContext().getTransactionEnvironment()
View Full Code Here


   * @param lce The entry representing the collection to add
   * @param persister The persister
   */
  private void addCollectionToCache(LoadingCollectionEntry lce, CollectionPersister persister) {
    final SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
    final SessionFactoryImplementor factory = session.getFactory();

    final boolean debugEnabled = LOG.isDebugEnabled();
    if ( debugEnabled ) {
      LOG.debugf( "Caching collection: %s", MessageHelper.collectionInfoString( persister, lce.getCollection(), lce.getKey(), session ) );
    }

    if ( !session.getEnabledFilters().isEmpty() && persister.isAffectedByEnabledFilters( session ) ) {
      // some filters affecting the collection are enabled on the session, so do not do the put into the cache.
      if ( debugEnabled ) {
        LOG.debug( "Refusing to add to cache due to enabled filters" );
      }
      // todo : add the notion of enabled filters to the CacheKey to differentiate filtered collections from non-filtered;
      //      but CacheKey is currently used for both collections and entities; would ideally need to define two seperate ones;
      //      currently this works in conjuction with the check on
      //      DefaultInitializeCollectionEventHandler.initializeCollectionFromCache() (which makes sure to not read from
      //      cache with enabled filters).
      // EARLY EXIT!!!!!
      return;
    }

    final Object version;
    if ( persister.isVersioned() ) {
      Object collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( lce.getKey(), persister );
      if ( collectionOwner == null ) {
        // generally speaking this would be caused by the collection key being defined by a property-ref, thus
        // the collection key and the owner key would not match up.  In this case, try to use the key of the
        // owner instance associated with the collection itself, if one.  If the collection does already know
        // about its owner, that owner should be the same instance as associated with the PC, but we do the
        // resolution against the PC anyway just to be safe since the lookup should not be costly.
        if ( lce.getCollection() != null ) {
          final Object linkedOwner = lce.getCollection().getOwner();
          if ( linkedOwner != null ) {
            final Serializable ownerKey = persister.getOwnerEntityPersister().getIdentifier( linkedOwner, session );
            collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( ownerKey, persister );
          }
        }
        if ( collectionOwner == null ) {
          throw new HibernateException(
              "Unable to resolve owner of loading collection [" +
                  MessageHelper.collectionInfoString( persister, lce.getCollection(), lce.getKey(), session ) +
                  "] for second level caching"
          );
        }
      }
      version = getLoadContext().getPersistenceContext().getEntry( collectionOwner ).getVersion();
    }
    else {
      version = null;
    }

    final CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister );
    final CacheKey cacheKey = session.generateCacheKey( lce.getKey(), persister.getKeyType(), persister.getRole() );

    try {
      session.getEventListenerManager().cachePutStart();
      final boolean put = persister.getCacheAccessStrategy().putFromLoad(
          cacheKey,
          persister.getCacheEntryStructure().structure( entry ),
          session.getTimestamp(),
          version,
          factory.getSettings().isMinimalPutsEnabled() && session.getCacheMode()!= CacheMode.REFRESH
      );

      if ( put && factory.getStatistics().isStatisticsEnabled() ) {
        factory.getStatisticsImplementor().secondLevelCachePut( persister.getCacheAccessStrategy().getRegion().getName() );
      }
    }
    finally {
      session.getEventListenerManager().cachePutEnd();
    }
View Full Code Here

    }
    if ( quantifier != null ) {
      buf.append( quantifier ).append( ' ' );
    }

    final SessionFactoryImplementor factory = criteriaQuery.getFactory();
    final OuterJoinLoadable persister =
        (OuterJoinLoadable) factory.getEntityPersister( criteriaImpl.getEntityOrClassName() );

    createAndSetInnerQuery( criteriaQuery, factory );
    criteriaImpl.setSession( deriveRootSession( criteria ) );

    final CriteriaJoinWalker walker = new CriteriaJoinWalker(
View Full Code Here

  @Override
  public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    //the following two lines were added to ensure that this.params is not null, which
    //can happen with two-deep nested subqueries
    final SessionFactoryImplementor factory = criteriaQuery.getFactory();
    createAndSetInnerQuery( criteriaQuery, factory );

    final Type[] ppTypes = params.getPositionalParameterTypes();
    final Object[] ppValues = params.getPositionalParameterValues();
    final TypedValue[] tv = new TypedValue[ppTypes.length];
View Full Code Here

    final StringBuilder fragment = new StringBuilder();

    if ( columns.length > 1 ) {
      fragment.append( '(' );
    }
    final SessionFactoryImplementor factory = criteriaQuery.getFactory();
    final int[] sqlTypes = type.sqlTypes( factory );
    for ( int i = 0; i < columns.length; i++ ) {
      final boolean lower = ignoreCase && (sqlTypes[i] == Types.VARCHAR || sqlTypes[i] == Types.CHAR);
      if ( lower ) {
        fragment.append( factory.getDialect().getLowercaseFunction() ).append( '(' );
      }
      fragment.append( columns[i] );
      if ( lower ) {
        fragment.append( ')' );
      }
View Full Code Here

   * @return The ORDER BY fragment for this ordering
   */
  public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) {
    final String[] columns = criteriaQuery.getColumnsUsingProjection( criteria, propertyName );
    final Type type = criteriaQuery.getTypeUsingProjection( criteria, propertyName );
    final SessionFactoryImplementor factory = criteriaQuery.getFactory();
    final int[] sqlTypes = type.sqlTypes( factory );

    final StringBuilder fragment = new StringBuilder();
    for ( int i=0; i<columns.length; i++ ) {
      final StringBuilder expression = new StringBuilder();
      boolean lower = false;
      if ( ignoreCase ) {
        final int sqlType = sqlTypes[i];
        lower = sqlType == Types.VARCHAR
            || sqlType == Types.CHAR
            || sqlType == Types.LONGVARCHAR;
      }
     
      if ( lower ) {
        expression.append( factory.getDialect().getLowercaseFunction() )
            .append( '(' );
      }
      expression.append( columns[i] );
      if ( lower ) {
        expression.append( ')' );
      }

      fragment.append(
          factory.getDialect().renderOrderByElement(
              expression.toString(),
              null,
              ascending ? "asc" : "desc",
              nullPrecedence != null ? nullPrecedence : factory.getSettings().getDefaultNullPrecedence()
          )
      );
      if ( i < columns.length-1 ) {
        fragment.append( ", " );
      }
View Full Code Here

  public void lock(Serializable id, Object version, Object object, int timeout, SessionImplementor session) {
    if ( !lockable.isVersioned() ) {
      throw new HibernateException( "write locks via update not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
    }

    final SessionFactoryImplementor factory = session.getFactory();
    try {
      try {
        final PreparedStatement st = session.getTransactionCoordinator().getJdbcCoordinator().getStatementPreparer().prepareStatement( sql );
        try {
          lockable.getVersionType().nullSafeSet( st, version, 1, session );
          int offset = 2;

          lockable.getIdentifierType().nullSafeSet( st, id, offset, session );
          offset += lockable.getIdentifierType().getColumnSpan( factory );

          if ( lockable.isVersioned() ) {
            lockable.getVersionType().nullSafeSet( st, version, offset, session );
          }

          final int affected = session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( st );
          // todo:  should this instead check for exactly one row modified?
          if ( affected < 0 ) {
            if (factory.getStatistics().isStatisticsEnabled()) {
              factory.getStatisticsImplementor().optimisticFailure( lockable.getEntityName() );
            }
            throw new StaleObjectStateException( lockable.getEntityName(), id );
          }

        }
View Full Code Here

      throw new PessimisticEntityLockException( object, "could not obtain pessimistic lock", e );
    }
  }

  protected String generateLockString() {
    final SessionFactoryImplementor factory = lockable.getFactory();
    final Update update = new Update( factory.getDialect() );
    update.setTableName( lockable.getRootTableName() );
    update.addPrimaryKeyColumns( lockable.getRootTableIdentifierColumnNames() );
    update.setVersionColumnName( lockable.getVersionColumnName() );
    update.addColumn( lockable.getVersionColumnName() );
    if ( factory.getSettings().isCommentsEnabled() ) {
      update.setComment( lockMode + " lock " + lockable.getEntityName() );
    }
    return update.toStatementString();
  }
View Full Code Here

      Object version,
      Object object,
      int timeout,
      SessionImplementor session) throws StaleObjectStateException, JDBCException {
    final String sql = determineSql( timeout );
    final SessionFactoryImplementor factory = session.getFactory();
    try {
      final PreparedStatement st = session.getTransactionCoordinator().getJdbcCoordinator().getStatementPreparer().prepareStatement( sql );
      try {
        getLockable().getIdentifierType().nullSafeSet( st, id, 1, session );
        if ( getLockable().isVersioned() ) {
          getLockable().getVersionType().nullSafeSet(
              st,
              version,
              getLockable().getIdentifierType().getColumnSpan( factory ) + 1,
              session
          );
        }

        final ResultSet rs = session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().extract( st );
        try {
          if ( !rs.next() ) {
            if ( factory.getStatistics().isStatisticsEnabled() ) {
              factory.getStatisticsImplementor()
                  .optimisticFailure( getLockable().getEntityName() );
            }
            throw new StaleObjectStateException( getLockable().getEntityName(), id );
          }
        }
View Full Code Here

        );
    }
  }

  protected String generateLockString(int timeout) {
    final SessionFactoryImplementor factory = getLockable().getFactory();
    final LockOptions lockOptions = new LockOptions( getLockMode() );
    lockOptions.setTimeOut( timeout );
    final SimpleSelect select = new SimpleSelect( factory.getDialect() )
        .setLockOptions( lockOptions )
        .setTableName( getLockable().getRootTableName() )
        .addColumn( getLockable().getRootTableIdentifierColumnNames()[0] )
        .addCondition( getLockable().getRootTableIdentifierColumnNames(), "=?" );
    if ( getLockable().isVersioned() ) {
      select.addCondition( getLockable().getVersionColumnName(), "=?" );
    }
    if ( factory.getSettings().isCommentsEnabled() ) {
      select.setComment( getLockMode() + " lock " + getLockable().getEntityName() );
    }
    return select.toStatementString();
  }
View Full Code Here

TOP

Related Classes of org.hibernate.engine.spi.SessionFactoryImplementor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.