Package org.hibernate.engine

Examples of org.hibernate.engine.ResultSetMappingDefinition


        log.debug("Checking named SQL query: " + queryName);
        // TODO : would be really nice to cache the spec on the query-def so as to not have to re-calc the hash;
        // currently not doable though because of the resultset-ref stuff...
        NativeSQLQuerySpecification spec;
        if ( qd.getResultSetRef() != null ) {
          ResultSetMappingDefinition definition = ( ResultSetMappingDefinition ) sqlResultSetMappings.get( qd.getResultSetRef() );
          if ( definition == null ) {
            throw new MappingException( "Unable to find resultset-ref definition: " + qd.getResultSetRef() );
          }
          spec = new NativeSQLQuerySpecification(
              qd.getQueryString(),
                  definition.getQueryReturns(),
                  qd.getQuerySpaces()
          );
        }
        else {
          spec =  new NativeSQLQuerySpecification(
View Full Code Here


   * @param parameterMetadata Metadata about parameters found in the query.
   */
  SQLQueryImpl(NamedSQLQueryDefinition queryDef, SessionImplementor session, ParameterMetadata parameterMetadata) {
    super( queryDef.getQueryString(), queryDef.getFlushMode(), session, parameterMetadata );
    if ( queryDef.getResultSetRef() != null ) {
      ResultSetMappingDefinition definition = session.getFactory()
          .getResultSetMapping( queryDef.getResultSetRef() );
      if (definition == null) {
        throw new MappingException(
            "Unable to find resultset-ref definition: " +
            queryDef.getResultSetRef()
          );
      }
      this.queryReturns = Arrays.asList( definition.getQueryReturns() );
    }
    else {
      this.queryReturns = Arrays.asList( queryDef.getQueryReturns() );
    }

View Full Code Here

  public SQLQuery addEntity(String alias, Class entityClass, LockMode lockMode) {
    return addEntity( alias, entityClass.getName(), lockMode );
  }

  public SQLQuery setResultSetMapping(String name) {
    ResultSetMappingDefinition mapping = session.getFactory().getResultSetMapping( name );
    if ( mapping == null ) {
      throw new MappingException( "Unknown SqlResultSetMapping [" + name + "]" );
    }
    NativeSQLQueryReturn[] returns = mapping.getQueryReturns();
    int length = returns.length;
    for ( int index = 0 ; index < length ; index++ ) {
      queryReturns.add( returns[index] );
    }
    return this;
View Full Code Here

          final NativeSQLQueryReturn[] queryReturns;
          if ( queryDefinition.getQueryReturns() != null ) {
            queryReturns = queryDefinition.getQueryReturns();
          }
          else if ( queryDefinition.getResultSetRef() != null ) {
            final ResultSetMappingDefinition rsMapping = factoryImplementor.getResultSetMapping(
                queryDefinition.getResultSetRef()
            );
            queryReturns = rsMapping.getQueryReturns();
          }
          else {
            throw new AssertionFailure( "Unsupported named query model. Please report the bug in Hibernate EntityManager");
          }
          if ( queryReturns.length > 1 ) {
View Full Code Here

          .setCallable( callable )
          .createNamedQueryDefinition();
      //TODO check there is no actual definition elemnents when a ref is defined
    }
    else {
      ResultSetMappingDefinition definition = buildResultSetMappingDefinition( queryElem, path, mappings );
      namedQuery = new NamedSQLQueryDefinitionBuilder().setName( queryName )
          .setQuery( queryElem.getText() )
          .setQueryReturns( definition.getQueryReturns() )
          .setQuerySpaces( synchronizedTables )
          .setCacheable( cacheable )
          .setCacheRegion( region )
          .setTimeout( timeout )
          .setFetchSize( fetchSize )
View Full Code Here

    this.path = path;
    this.mappings = mappings;
  }

  public void doSecondPass(Map persistentClasses) throws MappingException {
    ResultSetMappingDefinition definition = buildResultSetMappingDefinition( element, path, mappings);
    mappings.addResultSetMapping( definition );
  }
View Full Code Here

  protected static ResultSetMappingDefinition buildResultSetMappingDefinition(Element resultSetElem, String path, Mappings mappings) {
    String resultSetName = resultSetElem.attribute( "name" ).getValue();
    if ( path != null ) {
      resultSetName = path + '.' + resultSetName;
    }
    ResultSetMappingDefinition definition = new ResultSetMappingDefinition( resultSetName );

    int cnt = 0;
    Iterator returns = resultSetElem.elementIterator();
    while ( returns.hasNext() ) {
      cnt++;
      Element returnElem = (Element) returns.next();
      String name = returnElem.getName();
      if ( "return-scalar".equals( name ) ) {
        String column = returnElem.attributeValue( "column" );
        String typeFromXML = HbmBinder.getTypeFromXML( returnElem );
        Type type = null;
        if(typeFromXML!=null) {
          type = mappings.getTypeResolver().heuristicType( typeFromXML );
          if ( type == null ) {
            throw new MappingException( "could not determine type " + type );
          }
        }
        definition.addQueryReturn( new NativeSQLQueryScalarReturn( column, type ) );
      }
      else if ( "return".equals( name ) ) {
        definition.addQueryReturn( bindReturn( returnElem, mappings, cnt ) );
      }
      else if ( "return-join".equals( name ) ) {
        definition.addQueryReturn( bindReturnJoin( returnElem, mappings ) );
      }
      else if ( "load-collection".equals( name ) ) {
        definition.addQueryReturn( bindLoadCollection( returnElem, mappings ) );
      }
    }
    return definition;
  }
View Full Code Here

  }

  public void doSecondPass(Map persistentClasses) throws MappingException {
    //TODO add parameters checkings
    if ( ann == null ) return;
    ResultSetMappingDefinition definition = new ResultSetMappingDefinition( ann.name() );
    LOG.debugf( "Binding result set mapping: %s", definition.getName() );

    int entityAliasIndex = 0;

    for (EntityResult entity : ann.entities()) {
      //TODO parameterize lock mode?
      List<FieldResult> properties = new ArrayList<FieldResult>();
      List<String> propertyNames = new ArrayList<String>();
      for (FieldResult field : entity.fields()) {
        //use an ArrayList cause we might have several columns per root property
        String name = field.name();
        if ( name.indexOf( '.' ) == -1 ) {
          //regular property
          properties.add( field );
          propertyNames.add( name );
        }
        else {
          /**
           * Reorder properties
           * 1. get the parent property
           * 2. list all the properties following the expected one in the parent property
           * 3. calculate the lowest index and insert the property
           */
          PersistentClass pc = mappings.getClass( entity.entityClass().getName() );
          if ( pc == null ) {
            throw new MappingException(
                "Entity not found " + entity.entityClass().getName()
                    + " in SqlResultsetMapping " + ann.name()
            );
          }
          int dotIndex = name.lastIndexOf( '.' );
          String reducedName = name.substring( 0, dotIndex );
          Iterator parentPropIter = getSubPropertyIterator( pc, reducedName );
          List followers = getFollowers( parentPropIter, reducedName, name );

          int index = propertyNames.size();
          int followersSize = followers.size();
          for (int loop = 0; loop < followersSize; loop++) {
            String follower = (String) followers.get( loop );
            int currentIndex = getIndexOfFirstMatchingProperty( propertyNames, follower );
            index = currentIndex != -1 && currentIndex < index ? currentIndex : index;
          }
          propertyNames.add( index, name );
          properties.add( index, field );
        }
      }

      Set<String> uniqueReturnProperty = new HashSet<String>();
      Map<String, ArrayList<String>> propertyResultsTmp = new HashMap<String, ArrayList<String>>();
      for ( Object property : properties ) {
        final FieldResult propertyresult = ( FieldResult ) property;
        final String name = propertyresult.name();
        if ( "class".equals( name ) ) {
          throw new MappingException(
              "class is not a valid property name to use in a @FieldResult, use @Entity(discriminatorColumn) instead"
          );
        }

        if ( uniqueReturnProperty.contains( name ) ) {
          throw new MappingException(
              "duplicate @FieldResult for property " + name +
                  " on @Entity " + entity.entityClass().getName() + " in " + ann.name()
          );
        }
        uniqueReturnProperty.add( name );

        final String quotingNormalizedColumnName = mappings.getObjectNameNormalizer()
            .normalizeIdentifierQuoting( propertyresult.column() );

        String key = StringHelper.root( name );
        ArrayList<String> intermediateResults = propertyResultsTmp.get( key );
        if ( intermediateResults == null ) {
          intermediateResults = new ArrayList<String>();
          propertyResultsTmp.put( key, intermediateResults );
        }
        intermediateResults.add( quotingNormalizedColumnName );
      }

      Map<String, String[]> propertyResults = new HashMap<String,String[]>();
      for ( Map.Entry<String, ArrayList<String>> entry : propertyResultsTmp.entrySet() ) {
        propertyResults.put(
            entry.getKey(),
            entry.getValue().toArray( new String[ entry.getValue().size() ] )
        );
      }

      if ( !BinderHelper.isEmptyAnnotationValue( entity.discriminatorColumn() ) ) {
        final String quotingNormalizedName = mappings.getObjectNameNormalizer().normalizeIdentifierQuoting(
            entity.discriminatorColumn()
        );
        propertyResults.put( "class", new String[] { quotingNormalizedName } );
      }

      if ( propertyResults.isEmpty() ) {
        propertyResults = java.util.Collections.emptyMap();
      }

      NativeSQLQueryRootReturn result = new NativeSQLQueryRootReturn(
          "alias" + entityAliasIndex++,
          entity.entityClass().getName(),
          propertyResults,
          LockMode.READ
      );
      definition.addQueryReturn( result );
    }

    for ( ColumnResult column : ann.columns() ) {
      definition.addQueryReturn(
          new NativeSQLQueryScalarReturn(
              mappings.getObjectNameNormalizer().normalizeIdentifierQuoting(
                  column.name()
              ),
              null
          )
      );
    }

    for ( ConstructorResult constructorResult : ann.classes() ) {
      List<NativeSQLQueryScalarReturn> columnReturns = new ArrayList<NativeSQLQueryScalarReturn>();
      for ( ColumnResult columnResult : constructorResult.columns() ) {
        columnReturns.add(
            new NativeSQLQueryScalarReturn(
                mappings.getObjectNameNormalizer().normalizeIdentifierQuoting( columnResult.name() ),
                null
            )
        );
      }
      definition.addQueryReturn(
          new NativeSQLQueryConstructorReturn( constructorResult.targetClass(), columnReturns )
      );
    }

    if ( isDefault ) {
View Full Code Here

        LOG.debugf( "Checking named SQL query: %s", queryName );
        // TODO : would be really nice to cache the spec on the query-def so as to not have to re-calc the hash;
        // currently not doable though because of the resultset-ref stuff...
        NativeSQLQuerySpecification spec;
        if ( qd.getResultSetRef() != null ) {
          ResultSetMappingDefinition definition = sqlResultSetMappings.get( qd.getResultSetRef() );
          if ( definition == null ) {
            throw new MappingException( "Unable to find resultset-ref definition: " + qd.getResultSetRef() );
          }
          spec = new NativeSQLQuerySpecification(
              qd.getQueryString(),
                  definition.getQueryReturns(),
                  qd.getQuerySpaces()
          );
        }
        else {
          spec =  new NativeSQLQuerySpecification(
View Full Code Here

  }

  public void doSecondPass(Map persistentClasses) throws MappingException {
    //TODO add parameters checkings
    if ( ann == null ) return;
    ResultSetMappingDefinition definition = new ResultSetMappingDefinition( ann.name() );
    log.info( "Binding resultset mapping: {}", definition.getName() );

    int entityAliasIndex = 0;

    for (EntityResult entity : ann.entities()) {
      //TODO parameterize lock mode?
      List<FieldResult> properties = new ArrayList<FieldResult>();
      List<String> propertyNames = new ArrayList<String>();
      for (FieldResult field : entity.fields()) {
        //use an ArrayList cause we might have several columns per root property
        String name = field.name();
        if ( name.indexOf( '.' ) == -1 ) {
          //regular property
          properties.add( field );
          propertyNames.add( name );
        }
        else {
          /**
           * Reorder properties
           * 1. get the parent property
           * 2. list all the properties following the expected one in the parent property
           * 3. calculate the lowest index and insert the property
           */
          PersistentClass pc = mappings.getClass( entity.entityClass().getName() );
          if ( pc == null ) {
            throw new MappingException(
                "Entity not found " + entity.entityClass().getName()
                    + " in SqlResultsetMapping " + ann.name()
            );
          }
          int dotIndex = name.lastIndexOf( '.' );
          String reducedName = name.substring( 0, dotIndex );
          Iterator parentPropIter = getSubPropertyIterator( pc, reducedName );
          List followers = getFollowers( parentPropIter, reducedName, name );

          int index = propertyNames.size();
          int followersSize = followers.size();
          for (int loop = 0; loop < followersSize; loop++) {
            String follower = (String) followers.get( loop );
            int currentIndex = getIndexOfFirstMatchingProperty( propertyNames, follower );
            index = currentIndex != -1 && currentIndex < index ? currentIndex : index;
          }
          propertyNames.add( index, name );
          properties.add( index, field );
        }
      }

      Set<String> uniqueReturnProperty = new HashSet<String>();
      Map<String, ArrayList<String>> propertyResultsTmp = new HashMap<String, ArrayList<String>>();
      for ( Object property : properties ) {
        final FieldResult propertyresult = ( FieldResult ) property;
        final String name = propertyresult.name();
        if ( "class".equals( name ) ) {
          throw new MappingException(
              "class is not a valid property name to use in a @FieldResult, use @Entity(discriminatorColumn) instead"
          );
        }

        if ( uniqueReturnProperty.contains( name ) ) {
          throw new MappingException(
              "duplicate @FieldResult for property " + name +
                  " on @Entity " + entity.entityClass().getName() + " in " + ann.name()
          );
        }
        uniqueReturnProperty.add( name );

        final String quotingNormalizedColumnName = mappings.getObjectNameNormalizer()
            .normalizeIdentifierQuoting( propertyresult.column() );

        String key = StringHelper.root( name );
        ArrayList<String> intermediateResults = propertyResultsTmp.get( key );
        if ( intermediateResults == null ) {
          intermediateResults = new ArrayList<String>();
          propertyResultsTmp.put( key, intermediateResults );
        }
        intermediateResults.add( quotingNormalizedColumnName );
      }

      Map<String, String[]> propertyResults = new HashMap<String,String[]>();
      for ( Map.Entry<String, ArrayList<String>> entry : propertyResultsTmp.entrySet() ) {
        propertyResults.put(
            entry.getKey(),
            entry.getValue().toArray( new String[ entry.getValue().size() ] )
        );
      }

      if ( !BinderHelper.isDefault( entity.discriminatorColumn() ) ) {
        final String quotingNormalizedName = mappings.getObjectNameNormalizer().normalizeIdentifierQuoting(
            entity.discriminatorColumn()
        );
        propertyResults.put( "class", new String[] { quotingNormalizedName } );
      }

      if ( propertyResults.isEmpty() ) {
        propertyResults = java.util.Collections.emptyMap();
      }

      NativeSQLQueryRootReturn result = new NativeSQLQueryRootReturn(
          "alias" + entityAliasIndex++,
          entity.entityClass().getName(),
          propertyResults,
          LockMode.READ
      );
      definition.addQueryReturn( result );
    }

    for ( ColumnResult column : ann.columns() ) {
      definition.addQueryReturn(
          new NativeSQLQueryScalarReturn(
              mappings.getObjectNameNormalizer().normalizeIdentifierQuoting(
                  column.name()
              ),
              null
View Full Code Here

TOP

Related Classes of org.hibernate.engine.ResultSetMappingDefinition

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.