Package org.hibernate.engine.spi

Examples of org.hibernate.engine.spi.SessionFactoryImplementor


    this.entityName = persister.getRootEntityName();
    this.tenantId = session.getTenantIdentifier();

    this.naturalIdValues = new Serializable[naturalIdValues.length];

    final SessionFactoryImplementor factory = session.getFactory();
    final int[] naturalIdPropertyIndexes = persister.getNaturalIdentifierProperties();
    final Type[] propertyTypes = persister.getPropertyTypes();

    final int prime = 31;
    int result = 1;
View Full Code Here


        implicitParameterTypes.put( parameterName, javaType );
        return parameterName;
      }

      public String getCastType(Class javaType) {
        SessionFactoryImplementor factory =
            ( SessionFactoryImplementor ) entityManager.getFactory().getSessionFactory();
        Type hibernateType = factory.getTypeResolver().heuristicType( javaType.getName() );
        if ( hibernateType == null ) {
          throw new IllegalArgumentException(
              "Could not convert java type [" + javaType.getName() + "] to Hibernate type"
          );
        }
        int[] sqlTypeCodes = hibernateType.sqlTypes( factory );
        if ( sqlTypeCodes.length != 1 ) {
          throw new IllegalArgumentException(
              "Invalid Hibernate Type [" + hibernateType.getName() +
                  "] for cast : more than one column spanned"
          );
        }
        return factory.getDialect().getCastTypeName( sqlTypeCodes[0] );
      }
    };

    final RenderedCriteriaQuery renderedCriteriaQuery = criteriaQueryImpl.render( renderingContext );
View Full Code Here

          .ENTITY.equals( jpaType.getPersistenceType() )
          ? BindableType.ENTITY_TYPE
          : BindableType.SINGULAR_ATTRIBUTE;

      String guessedRoleName = determineRole( attribute );
      SessionFactoryImplementor sfi = (SessionFactoryImplementor)
          criteriaBuilder.getEntityManagerFactory().getSessionFactory();
      mapPersister = sfi.getCollectionPersister( guessedRoleName );
      if ( mapPersister == null ) {
        throw new IllegalStateException( "Could not locate collection persister [" + guessedRoleName + "]" );
      }
      mapKeyType = mapPersister.getIndexType();
      if ( mapKeyType == null ) {
View Full Code Here

    this.attribute = attribute;
    this.persister = resolvePersister( criteriaBuilder, attribute );
  }

  private static CollectionPersister resolvePersister(CriteriaBuilderImpl criteriaBuilder, PluralAttribute attribute) {
    SessionFactoryImplementor sfi = (SessionFactoryImplementor)
        criteriaBuilder.getEntityManagerFactory().getSessionFactory();
    return sfi.getCollectionPersister( resolveRole( attribute ) );
  }
View Full Code Here

  private List<ParameterSpecification> parameterSpecifications;

  public DeleteExecutor(HqlSqlWalker walker, Queryable persister) {
    super( walker, persister );
   
    final SessionFactoryImplementor factory = walker.getSessionFactoryHelper().getFactory();
    final Dialect dialect = factory.getDialect();
   
    try {
      final DeleteStatement deleteStatement = (DeleteStatement) walker.getAST();
     
      final String idSubselectWhere;
      if ( deleteStatement.hasWhereClause() ) {
        final AST whereClause = deleteStatement.getWhereClause();
        final SqlGenerator gen = new SqlGenerator( factory );
        gen.whereClause( whereClause );
        parameterSpecifications = gen.getCollectedParameters();
        idSubselectWhere = gen.getSQL().length() > 7 ? gen.getSQL() : "";
      }
      else {
        parameterSpecifications = new ArrayList<ParameterSpecification>();
        idSubselectWhere = "";
      }
     
      // If many-to-many, delete the FK row in the collection table.
      for ( Type type : persister.getPropertyTypes() ) {
        if ( type.isCollectionType() ) {
          final CollectionType cType = (CollectionType) type;
          final AbstractCollectionPersister cPersister = (AbstractCollectionPersister) factory
              .getCollectionPersister( cType.getRole() );
          if ( cPersister.isManyToMany() ) {
            if ( persister.getIdentifierColumnNames().length > 1
                && !dialect.supportsTuplesInSubqueries() ) {
              LOG.warn(
                  "This dialect is unable to cascade the delete into the many-to-many join table" +
                  " when the entity has multiple primary keys.  Either properly setup cascading on" +
                  " the constraints or manually clear the associations prior to deleting the entities."
              );
            }
            else {
              final String idSubselect = "(select "
                  + StringHelper.join( ", ", persister.getIdentifierColumnNames() ) + " from "
                  + persister.getTableName() + idSubselectWhere + ")";
              final String where = "(" + StringHelper.join( ", ", cPersister.getKeyColumnNames() )
                  + ") in " + idSubselect;
              final Delete delete = new Delete().setTableName( cPersister.getTableName() ).setWhere( where );
              if ( factory.getSettings().isCommentsEnabled() ) {
                delete.setComment( "delete FKs in join table" );
              }
              deletes.add( delete.toStatementString() );
            }
          }
View Full Code Here

    eventListenerRegistry.appendListeners( EventType.POST_UPDATE, this );
  }

  private void evictCache(Object entity, EntityPersister persister, EventSource session, Object[] oldState) {
    try {
      SessionFactoryImplementor factory = persister.getFactory();

      Set<String> collectionRoles = factory.getCollectionRolesByEntityParticipant( persister.getEntityName() );
      if ( collectionRoles == null || collectionRoles.isEmpty() ) {
        return;
      }
      for ( String role : collectionRoles ) {
        CollectionPersister collectionPersister = factory.getCollectionPersister( role );
        if ( !collectionPersister.hasCache() ) {
          // ignore collection if no caching is used
          continue;
        }
        // this is the property this OneToMany relation is mapped by
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();

    if ( LOG.isDebugEnabled() ) {
      LOG.debugf( "Caching collection: %s", MessageHelper.collectionInfoString( persister, lce.getKey(), factory ) );
    }

    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.
      LOG.debugf( "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).
      return; // EARLY EXIT!!!!!
    }

    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 ) {
          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.getKey(), factory ) +
                  "] for second level caching"
          );
        }
      }
      version = getLoadContext().getPersistenceContext().getEntry( collectionOwner ).getVersion();
    }
    else {
      version = null;
    }

    CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister );
    CacheKey cacheKey = session.generateCacheKey( lce.getKey(), persister.getKeyType(), persister.getRole() );
    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() );
    }
  }
View Full Code Here

      Object version,
      Object object,
      int timeout,
      SessionImplementor session) throws StaleObjectStateException, JDBCException {
    final String sql = determineSql( timeout );
    SessionFactoryImplementor factory = session.getFactory();
    try {
      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
          );
        }

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

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

  protected String generateLockString(int lockTimeout) {
    SessionFactoryImplementor factory = getLockable().getFactory();
    LockOptions lockOptions = new LockOptions( getLockMode() );
    lockOptions.setTimeOut( lockTimeout );
    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

          SessionImplementor session) throws StaleObjectStateException, JDBCException {
    if ( !lockable.isVersioned() ) {
      throw new HibernateException( "write locks via update not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
    }
    // todo : should we additionally check the current isolation mode explicitly?
    SessionFactoryImplementor factory = session.getFactory();
    try {
      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 );
        }

        int affected = st.executeUpdate();
        if ( affected < 0 ) {
          factory.getStatisticsImplementor().optimisticFailure( lockable.getEntityName() );
          throw new StaleObjectStateException( lockable.getEntityName(), id );
        }

      }
      finally {
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.