Package org.hibernate.cfg.annotations

Examples of org.hibernate.cfg.annotations.EntityBinder


    PersistentClass persistentClass = makePersistentClass( inheritanceState, superEntity );
    Entity entityAnn = clazzToProcess.getAnnotation( Entity.class );
    org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation(
        org.hibernate.annotations.Entity.class
    );
    EntityBinder entityBinder = new EntityBinder(
        entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
    );
    entityBinder.setInheritanceState( inheritanceState );

    bindQueries( clazzToProcess, mappings );
    bindFilterDefs( clazzToProcess, mappings );
    bindTypeDefs( clazzToProcess, mappings );
    bindFetchProfiles( clazzToProcess, mappings );
    BinderHelper.bindAnyMetaDefs( clazzToProcess, mappings );

    String schema = "";
    String table = ""; //might be no @Table annotation on the annotated class
    String catalog = "";
    List<UniqueConstraintHolder> uniqueConstraints = new ArrayList<UniqueConstraintHolder>();
    javax.persistence.Table tabAnn = null;
    if ( clazzToProcess.isAnnotationPresent( javax.persistence.Table.class ) ) {
      tabAnn = clazzToProcess.getAnnotation( javax.persistence.Table.class );
      table = tabAnn.name();
      schema = tabAnn.schema();
      catalog = tabAnn.catalog();
      uniqueConstraints = TableBinder.buildUniqueConstraintHolders( tabAnn.uniqueConstraints() );
    }

    Ejb3JoinColumn[] inheritanceJoinedColumns = makeInheritanceJoinColumns(
        clazzToProcess, mappings, inheritanceState, superEntity
    );

    final Ejb3DiscriminatorColumn discriminatorColumn;
    if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      discriminatorColumn = processSingleTableDiscriminatorProperties(
          clazzToProcess,
          mappings,
          inheritanceState,
          entityBinder
      );
    }
    else if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) ) {
      discriminatorColumn = processJoinedDiscriminatorProperties(
          clazzToProcess,
          mappings,
          inheritanceState,
          entityBinder
      );
    }
    else {
      discriminatorColumn = null;
    }

    entityBinder.setProxy( clazzToProcess.getAnnotation( Proxy.class ) );
    entityBinder.setBatchSize( clazzToProcess.getAnnotation( BatchSize.class ) );
    entityBinder.setWhere( clazzToProcess.getAnnotation( Where.class ) );
      entityBinder.setCache( determineCacheSettings( clazzToProcess, mappings ) );
      entityBinder.setNaturalIdCache( clazzToProcess, clazzToProcess.getAnnotation( NaturalIdCache.class ) );

    bindFilters( clazzToProcess, entityBinder, mappings );

    entityBinder.bindEntity();

    if ( inheritanceState.hasTable() ) {
      Check checkAnn = clazzToProcess.getAnnotation( Check.class );
      String constraints = checkAnn == null ?
          null :
          checkAnn.constraints();
      entityBinder.bindTable(
          schema, catalog, table, uniqueConstraints,
          constraints, inheritanceState.hasDenormalizedTable() ?
              superEntity.getTable() :
              null
      );
    }
    else if ( clazzToProcess.isAnnotationPresent( Table.class ) ) {
      LOG.invalidTableAnnotation( clazzToProcess.getName() );
    }


    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(
        clazzToProcess,
        persistentClass,
        entityBinder, mappings, inheritanceStatePerClass
    );

    javax.persistence.SecondaryTable secTabAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTable.class
    );
    javax.persistence.SecondaryTables secTabsAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTables.class
    );
    entityBinder.firstLevelSecondaryTablesBinding( secTabAnn, secTabsAnn );

    OnDelete onDeleteAnn = clazzToProcess.getAnnotation( OnDelete.class );
    boolean onDeleteAppropriate = false;

    // todo : sucks that this is separate from RootClass distinction
    final boolean isInheritanceRoot = !inheritanceState.hasParents();
    final boolean hasSubclasses = inheritanceState.hasSiblings();

    if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) ) {
      if ( inheritanceState.hasParents() ) {
        onDeleteAppropriate = true;
        final JoinedSubclass jsc = ( JoinedSubclass ) persistentClass;
        SimpleValue key = new DependantValue( mappings, jsc.getTable(), jsc.getIdentifier() );
        jsc.setKey( key );
        ForeignKey fk = clazzToProcess.getAnnotation( ForeignKey.class );
        if ( fk != null && !BinderHelper.isEmptyAnnotationValue( fk.name() ) ) {
          key.setForeignKeyName( fk.name() );
        }
        if ( onDeleteAnn != null ) {
          key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
        }
        else {
          key.setCascadeDeleteEnabled( false );
        }
        //we are never in a second pass at that stage, so queue it
        SecondPass sp = new JoinedSubclassFkSecondPass( jsc, inheritanceJoinedColumns, key, mappings );
        mappings.addSecondPass( sp );
        mappings.addSecondPass( new CreateKeySecondPass( jsc ) );
      }

      if ( isInheritanceRoot ) {
        // the class we are processing is the root of the hierarchy, see if we had a discriminator column
        // (it is perfectly valid for joined subclasses to not have discriminators).
        if ( discriminatorColumn != null ) {
          // we have a discriminator column
          if ( hasSubclasses || !discriminatorColumn.isImplicit() ) {
            bindDiscriminatorColumnToRootPersistentClass(
                (RootClass) persistentClass,
                discriminatorColumn,
                entityBinder.getSecondaryTables(),
                propertyHolder,
                mappings
            );
            //bind it again since the type might have changed
            entityBinder.bindDiscriminatorValue();
          }
        }
      }
    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      if ( isInheritanceRoot ) {
        if ( hasSubclasses || !discriminatorColumn.isImplicit() ) {
          bindDiscriminatorColumnToRootPersistentClass(
              (RootClass) persistentClass,
              discriminatorColumn,
              entityBinder.getSecondaryTables(),
              propertyHolder,
              mappings
          );
          //bind it again since the type might have changed
          entityBinder.bindDiscriminatorValue();
        }
      }
    }

        if ( onDeleteAnn != null && !onDeleteAppropriate ) {
      LOG.invalidOnDeleteAnnotation(propertyHolder.getEntityName());
    }

    // try to find class level generators
    HashMap<String, IdGenerator> classGenerators = buildLocalGenerators( clazzToProcess, mappings );

    // check properties
    final InheritanceState.ElementsToProcess elementsToProcess = inheritanceState.getElementsToProcess();
    inheritanceState.postProcess( persistentClass, entityBinder );

    final boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE
        && inheritanceState.hasParents();
    Set<String> idPropertiesIfIdClass = new HashSet<String>();
    boolean isIdClass = mapAsIdClass(
        inheritanceStatePerClass,
        inheritanceState,
        persistentClass,
        entityBinder,
        propertyHolder,
        elementsToProcess,
        idPropertiesIfIdClass,
        mappings
    );

    if ( !isIdClass ) {
      entityBinder.setWrapIdsInEmbeddedComponents( elementsToProcess.getIdPropertyCount() > 1 );
    }

    processIdPropertiesIfNotAlready(
        inheritanceStatePerClass,
        mappings,
        persistentClass,
        entityBinder,
        propertyHolder,
        classGenerators,
        elementsToProcess,
        subclassAndSingleTableStrategy,
        idPropertiesIfIdClass
    );

    if ( !inheritanceState.hasParents() ) {
      final RootClass rootClass = ( RootClass ) persistentClass;
      mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
    }
    else {
      superEntity.addSubclass( ( Subclass ) persistentClass );
    }

    mappings.addClass( persistentClass );

    //Process secondary tables and complementary definitions (ie o.h.a.Table)
    mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder, clazzToProcess ) );

    //add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Table.class ) );
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Tables.class ) );
    entityBinder.processComplementaryTableDefinitions( tabAnn );
  }
View Full Code Here


        org.hibernate.annotations.Entity.class
    );
    org.hibernate.annotations.Cache cacheAnn = annotatedClass.getAnnotation(
        org.hibernate.annotations.Cache.class
    );
    EntityBinder entityBinder = new EntityBinder(
        entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
    );
    entityBinder.setDiscriminatorValue( discrimValue );
    entityBinder.setBatchSize( sizeAnn );
    entityBinder.setProxy( proxyAnn );
    entityBinder.setWhere( whereAnn );
    entityBinder.setCache( cacheAnn );
    entityBinder.setInheritanceState( inheritanceState );
    Filter filterAnn = annotatedClass.getAnnotation( Filter.class );
    if ( filterAnn != null ) {
      entityBinder.addFilter( filterAnn.name(), filterAnn.condition() );
    }
    Filters filtersAnn = annotatedClass.getAnnotation( Filters.class );
    if ( filtersAnn != null ) {
      for (Filter filter : filtersAnn.value()) {
        entityBinder.addFilter( filter.name(), filter.condition() );
      }
    }
    entityBinder.bindEntity();

    if ( inheritanceState.hasTable() ) {
      Check checkAnn = annotatedClass.getAnnotation( Check.class );
      String constraints = checkAnn == null ?
          null :
          checkAnn.constraints();
      entityBinder.bindTable(
          schema, catalog, table, uniqueConstraints,
          constraints, inheritanceState.hasDenormalizedTable() ?
          superEntity.getTable() :
          null
      );
    }
    else {
      if ( annotatedClass.isAnnotationPresent( Table.class ) ) {
        log.warn( "Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: " + clazzToProcess
            .getName() );
      }
    }
//    Map<String, Column[]> columnOverride = PropertyHolderBuilder.buildHierarchyColumnOverride(
//        clazzToProcess,
//        persistentClass.getClassName()
//    );
    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(
        clazzToProcess,
        persistentClass,
        entityBinder, mappings
    );

    javax.persistence.SecondaryTable secTabAnn = annotatedClass.getAnnotation(
        javax.persistence.SecondaryTable.class
    );
    javax.persistence.SecondaryTables secTabsAnn = annotatedClass.getAnnotation(
        javax.persistence.SecondaryTables.class
    );
    entityBinder.firstLevelSecondaryTablesBinding( secTabAnn, secTabsAnn );

    OnDelete onDeleteAnn = annotatedClass.getAnnotation( OnDelete.class );
    boolean onDeleteAppropriate = false;
    if ( InheritanceType.JOINED.equals( inheritanceState.type ) && inheritanceState.hasParents ) {
      onDeleteAppropriate = true;
      final JoinedSubclass jsc = (JoinedSubclass) persistentClass;
      if ( persistentClass.getEntityPersisterClass() == null ) {
        persistentClass.getRootClass().setEntityPersisterClass( JoinedSubclassEntityPersister.class );
      }
      SimpleValue key = new DependantValue( jsc.getTable(), jsc.getIdentifier() );
      jsc.setKey( key );
      ForeignKey fk = annotatedClass.getAnnotation( ForeignKey.class );
      if ( fk != null && !BinderHelper.isDefault( fk.name() ) ) {
        key.setForeignKeyName( fk.name() );
      }
      if ( onDeleteAnn != null ) {
        key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
      }
      else {
        key.setCascadeDeleteEnabled( false );
      }
      //we are never in a second pass at that stage, so queue it
      SecondPass sp = new JoinedSubclassFkSecondPass( jsc, inheritanceJoinedColumns, key, mappings );
      mappings.addSecondPass( sp );
      mappings.addSecondPass( new CreateKeySecondPass( jsc ) );

    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.type ) ) {
      if ( inheritanceState.hasParents ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( SingleTableEntityPersister.class );
        }
      }
      else {
        if ( inheritanceState.hasSons || !discriminatorColumn.isImplicit() ) {
          //need a discriminator column
          bindDiscriminatorToPersistentClass(
              (RootClass) persistentClass,
              discriminatorColumn,
              entityBinder.getSecondaryTables(),
              propertyHolder
          );
          entityBinder.bindDiscriminatorValue();//bind it again since the type might have changed
        }
      }
    }
    else if ( InheritanceType.TABLE_PER_CLASS.equals( inheritanceState.type ) ) {
      if ( inheritanceState.hasParents ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( UnionSubclassEntityPersister.class );
        }
      }
    }
    if ( onDeleteAnn != null && !onDeleteAppropriate ) {
      log.warn(
          "Inapropriate use of @OnDelete on entity, annotation ignored: {}", propertyHolder.getEntityName()
      );
    }

    //try to find class level generators
    HashMap<String, IdGenerator> classGenerators = buildLocalGenerators( annotatedClass, mappings );

    // check properties
    List<PropertyData> elements =
        getElementsToProcess(
            clazzToProcess, inheritanceStatePerClass, propertyHolder, entityBinder, mappings
        );
    if ( elements == null ) {
      throw new AnnotationException( "No identifier specified for entity: " + propertyHolder.getEntityName() );
    }
    final boolean subclassAndSingleTableStrategy = inheritanceState.type == InheritanceType.SINGLE_TABLE
        && inheritanceState.hasParents;
    //process idclass if any
    Set<String> idProperties = new HashSet<String>();
    IdClass idClass = null;
    if ( !inheritanceState.hasParents ) {
      //look for idClass
      XClass current = inheritanceState.clazz;
      InheritanceState state = inheritanceState;
      do {
        current = state.clazz;
        if ( current.isAnnotationPresent( IdClass.class ) ) {
          idClass = current.getAnnotation( IdClass.class );
          break;
        }
        state = InheritanceState.getSuperclassInheritanceState(
            current, inheritanceStatePerClass, mappings.getReflectionManager()
        );
      }
      while ( state != null );
    }
    if ( idClass != null ) {
      XClass compositeClass = mappings.getReflectionManager().toXClass( idClass.value() );
      boolean isComponent = true;
      boolean propertyAnnotated = entityBinder.isPropertyAnnotated( compositeClass );
      String propertyAccessor = entityBinder.getPropertyAccessor( compositeClass );
      String generatorType = "assigned";
      String generator = BinderHelper.ANNOTATION_STRING_DEFAULT;
      PropertyData inferredData = new PropertyPreloadedData(
          entityBinder.getPropertyAccessor(), "id", compositeClass
      );
      HashMap<String, IdGenerator> localGenerators = new HashMap<String, IdGenerator>();
      boolean ignoreIdAnnotations = entityBinder.isIgnoreIdAnnotations();
      entityBinder.setIgnoreIdAnnotations( true );
      bindId(
          generatorType,
          generator,
          inferredData,
          null,
          propertyHolder,
          localGenerators,
          isComponent,
          propertyAnnotated,
          propertyAccessor, entityBinder,
          true,
          false, mappings
      );
      inferredData = new PropertyPreloadedData(
          propertyAccessor, "_identifierMapper", compositeClass
      );
      Component mapper = fillComponent(
          propertyHolder,
          inferredData,
          propertyAnnotated,
          propertyAccessor, false,
          entityBinder,
          true, true,
          false, mappings
      );
      entityBinder.setIgnoreIdAnnotations( ignoreIdAnnotations );
      persistentClass.setIdentifierMapper( mapper );
      Property property = new Property();
      property.setName( "_identifierMapper" );
      property.setNodeName( "id" );
      property.setUpdateable( false );
      property.setInsertable( false );
      property.setValue( mapper );
      property.setPropertyAccessorName( "embedded" );
      persistentClass.addProperty( property );
      entityBinder.setIgnoreIdAnnotations( true );

      Iterator properties = mapper.getPropertyIterator();
      while ( properties.hasNext() ) {
        idProperties.add( ( (Property) properties.next() ).getName() );
      }
    }
    Set<String> missingIdProperties = new HashSet<String>( idProperties );
    for (PropertyData propertyAnnotatedElement : elements) {
      String propertyName = propertyAnnotatedElement.getPropertyName();
      if ( !idProperties.contains( propertyName ) ) {
        processElementAnnotations(
            propertyHolder,
            subclassAndSingleTableStrategy ?
                Nullability.FORCED_NULL :
                Nullability.NO_CONSTRAINT,
            propertyAnnotatedElement.getProperty(),
            propertyAnnotatedElement, classGenerators, entityBinder,
            false, false, false, mappings
        );
      }
      else {
        missingIdProperties.remove( propertyName );
      }
    }

    if ( missingIdProperties.size() != 0 ) {
      StringBuilder missings = new StringBuilder();
      for (String property : missingIdProperties) {
        missings.append( property ).append( ", " );
      }
      throw new AnnotationException(
          "Unable to find properties ("
              + missings.substring( 0, missings.length() - 2 )
              + ") in entity annotated with @IdClass:" + persistentClass.getEntityName()
      );
    }

    if ( !inheritanceState.hasParents ) {
      final RootClass rootClass = (RootClass) persistentClass;
      mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
    }
    else {
      superEntity.addSubclass( (Subclass) persistentClass );
    }

    mappings.addClass( persistentClass );

    //Process secondary tables and complementary definitions (ie o.h.a.Table)
    mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder, annotatedClass ) );

    //add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions( annotatedClass.getAnnotation( org.hibernate.annotations.Table.class ) );
    entityBinder.processComplementaryTableDefinitions( annotatedClass.getAnnotation( org.hibernate.annotations.Tables.class ) );

  }
View Full Code Here

    PersistentClass persistentClass = makePersistentClass( inheritanceState, superEntity );
    Entity entityAnn = clazzToProcess.getAnnotation( Entity.class );
    org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation(
        org.hibernate.annotations.Entity.class
    );
    EntityBinder entityBinder = new EntityBinder(
        entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
    );
    entityBinder.setInheritanceState( inheritanceState );

    bindQueries( clazzToProcess, mappings );
    bindFilterDefs( clazzToProcess, mappings );
    bindTypeDefs( clazzToProcess, mappings );
    bindFetchProfiles( clazzToProcess, mappings );
    BinderHelper.bindAnyMetaDefs( clazzToProcess, mappings );

    String schema = "";
    String table = ""; //might be no @Table annotation on the annotated class
    String catalog = "";
    List<UniqueConstraintHolder> uniqueConstraints = new ArrayList<UniqueConstraintHolder>();
    if ( clazzToProcess.isAnnotationPresent( javax.persistence.Table.class ) ) {
      javax.persistence.Table tabAnn = clazzToProcess.getAnnotation( javax.persistence.Table.class );
      table = tabAnn.name();
      schema = tabAnn.schema();
      catalog = tabAnn.catalog();
      uniqueConstraints = TableBinder.buildUniqueConstraintHolders( tabAnn.uniqueConstraints() );
    }

    Ejb3JoinColumn[] inheritanceJoinedColumns = makeInheritanceJoinColumns(
        clazzToProcess, mappings, inheritanceState, superEntity
    );
    Ejb3DiscriminatorColumn discriminatorColumn = null;
    if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      discriminatorColumn = processDiscriminatorProperties(
          clazzToProcess, mappings, inheritanceState, entityBinder
      );
    }

    entityBinder.setProxy( clazzToProcess.getAnnotation( Proxy.class ) );
    entityBinder.setBatchSize( clazzToProcess.getAnnotation( BatchSize.class ) );
    entityBinder.setWhere( clazzToProcess.getAnnotation( Where.class ) );
      entityBinder.setCache( determineCacheSettings( clazzToProcess, mappings ) );

    //Filters are not allowed on subclasses
    if ( !inheritanceState.hasParents() ) {
      bindFilters( clazzToProcess, entityBinder, mappings );
    }

    entityBinder.bindEntity();

    if ( inheritanceState.hasTable() ) {
      Check checkAnn = clazzToProcess.getAnnotation( Check.class );
      String constraints = checkAnn == null ?
          null :
          checkAnn.constraints();
      entityBinder.bindTable(
          schema, catalog, table, uniqueConstraints,
          constraints, inheritanceState.hasDenormalizedTable() ?
              superEntity.getTable() :
              null
      );
    }
    else if ( clazzToProcess.isAnnotationPresent( Table.class ) ) {
      LOG.invalidTableAnnotation( clazzToProcess.getName() );
    }


    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(
        clazzToProcess,
        persistentClass,
        entityBinder, mappings, inheritanceStatePerClass
    );

    javax.persistence.SecondaryTable secTabAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTable.class
    );
    javax.persistence.SecondaryTables secTabsAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTables.class
    );
    entityBinder.firstLevelSecondaryTablesBinding( secTabAnn, secTabsAnn );

    OnDelete onDeleteAnn = clazzToProcess.getAnnotation( OnDelete.class );
    boolean onDeleteAppropriate = false;
    if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) && inheritanceState.hasParents() ) {
      onDeleteAppropriate = true;
      final JoinedSubclass jsc = ( JoinedSubclass ) persistentClass;
      SimpleValue key = new DependantValue( mappings, jsc.getTable(), jsc.getIdentifier() );
      jsc.setKey( key );
      ForeignKey fk = clazzToProcess.getAnnotation( ForeignKey.class );
      if ( fk != null && !BinderHelper.isEmptyAnnotationValue( fk.name() ) ) {
        key.setForeignKeyName( fk.name() );
      }
      if ( onDeleteAnn != null ) {
        key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
      }
      else {
        key.setCascadeDeleteEnabled( false );
      }
      //we are never in a second pass at that stage, so queue it
      SecondPass sp = new JoinedSubclassFkSecondPass( jsc, inheritanceJoinedColumns, key, mappings );
      mappings.addSecondPass( sp );
      mappings.addSecondPass( new CreateKeySecondPass( jsc ) );

    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      if ( ! inheritanceState.hasParents() ) {
        if ( inheritanceState.hasSiblings() || !discriminatorColumn.isImplicit() ) {
          //need a discriminator column
          bindDiscriminatorToPersistentClass(
              ( RootClass ) persistentClass,
              discriminatorColumn,
              entityBinder.getSecondaryTables(),
              propertyHolder,
              mappings
          );
          entityBinder.bindDiscriminatorValue();//bind it again since the type might have changed
        }
      }
    }
    else if ( InheritanceType.TABLE_PER_CLASS.equals( inheritanceState.getType() ) ) {
      //nothing to do
    }
        if (onDeleteAnn != null && !onDeleteAppropriate) LOG.invalidOnDeleteAnnotation(propertyHolder.getEntityName());

    // try to find class level generators
    HashMap<String, IdGenerator> classGenerators = buildLocalGenerators( clazzToProcess, mappings );

    // check properties
    final InheritanceState.ElementsToProcess elementsToProcess = inheritanceState.getElementsToProcess();
    inheritanceState.postProcess( persistentClass, entityBinder );

    final boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE
        && inheritanceState.hasParents();
    Set<String> idPropertiesIfIdClass = new HashSet<String>();
    boolean isIdClass = mapAsIdClass(
        inheritanceStatePerClass,
        inheritanceState,
        persistentClass,
        entityBinder,
        propertyHolder,
        elementsToProcess,
        idPropertiesIfIdClass,
        mappings
    );

    if ( !isIdClass ) {
      entityBinder.setWrapIdsInEmbeddedComponents( elementsToProcess.getIdPropertyCount() > 1 );
    }

    processIdPropertiesIfNotAlready(
        inheritanceStatePerClass,
        mappings,
        persistentClass,
        entityBinder,
        propertyHolder,
        classGenerators,
        elementsToProcess,
        subclassAndSingleTableStrategy,
        idPropertiesIfIdClass
    );

    if ( !inheritanceState.hasParents() ) {
      final RootClass rootClass = ( RootClass ) persistentClass;
      mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
    }
    else {
      superEntity.addSubclass( ( Subclass ) persistentClass );
    }

    mappings.addClass( persistentClass );

    //Process secondary tables and complementary definitions (ie o.h.a.Table)
    mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder, clazzToProcess ) );

    //add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Table.class ) );
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Tables.class ) );

  }
View Full Code Here

        org.hibernate.annotations.Entity.class
    );

    Cache cacheAnn = determineCacheSettings( clazzToProcess, mappings );

    EntityBinder entityBinder = new EntityBinder(
        entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
    );
    entityBinder.setDiscriminatorValue( discrimValue );
    entityBinder.setBatchSize( sizeAnn );
    entityBinder.setProxy( proxyAnn );
    entityBinder.setWhere( whereAnn );
    entityBinder.setCache( cacheAnn );
    entityBinder.setInheritanceState( inheritanceState );

    //Filters are not allowed on subclasses
    if ( !inheritanceState.hasParents() ) {
      bindFilters(clazzToProcess, entityBinder, mappings);
    }

    entityBinder.bindEntity();

    if ( inheritanceState.hasTable() ) {
      Check checkAnn = clazzToProcess.getAnnotation( Check.class );
      String constraints = checkAnn == null ?
          null :
          checkAnn.constraints();
      entityBinder.bindTable(
          schema, catalog, table, uniqueConstraints,
          constraints, inheritanceState.hasDenormalizedTable() ?
          superEntity.getTable() :
          null
      );
    }
    else {
      if ( clazzToProcess.isAnnotationPresent( Table.class ) ) {
        log.warn( "Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: " + clazzToProcess
            .getName() );
      }
    }

    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(
        clazzToProcess,
        persistentClass,
        entityBinder, mappings, inheritanceStatePerClass
    );

    javax.persistence.SecondaryTable secTabAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTable.class
    );
    javax.persistence.SecondaryTables secTabsAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTables.class
    );
    entityBinder.firstLevelSecondaryTablesBinding( secTabAnn, secTabsAnn );

    OnDelete onDeleteAnn = clazzToProcess.getAnnotation( OnDelete.class );
    boolean onDeleteAppropriate = false;
    if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) && inheritanceState.hasParents() ) {
      onDeleteAppropriate = true;
      final JoinedSubclass jsc = (JoinedSubclass) persistentClass;
      if ( persistentClass.getEntityPersisterClass() == null ) {
        persistentClass.getRootClass().setEntityPersisterClass( JoinedSubclassEntityPersister.class );
      }
      SimpleValue key = new DependantValue( jsc.getTable(), jsc.getIdentifier() );
      jsc.setKey( key );
      ForeignKey fk = clazzToProcess.getAnnotation( ForeignKey.class );
      if ( fk != null && !BinderHelper.isDefault( fk.name() ) ) {
        key.setForeignKeyName( fk.name() );
      }
      if ( onDeleteAnn != null ) {
        key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
      }
      else {
        key.setCascadeDeleteEnabled( false );
      }
      //we are never in a second pass at that stage, so queue it
      SecondPass sp = new JoinedSubclassFkSecondPass( jsc, inheritanceJoinedColumns, key, mappings );
      mappings.addSecondPass( sp );
      mappings.addSecondPass( new CreateKeySecondPass( jsc ) );

    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      if ( inheritanceState.hasParents() ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( SingleTableEntityPersister.class );
        }
      }
      else {
        if ( inheritanceState.hasSiblings() || !discriminatorColumn.isImplicit() ) {
          //need a discriminator column
          bindDiscriminatorToPersistentClass(
              (RootClass) persistentClass,
              discriminatorColumn,
              entityBinder.getSecondaryTables(),
              propertyHolder
          );
          entityBinder.bindDiscriminatorValue();//bind it again since the type might have changed
        }
      }
    }
    else if ( InheritanceType.TABLE_PER_CLASS.equals( inheritanceState.getType() ) ) {
      if ( inheritanceState.hasParents() ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( UnionSubclassEntityPersister.class );
        }
      }
    }
    if ( onDeleteAnn != null && !onDeleteAppropriate ) {
      log.warn(
          "Inapropriate use of @OnDelete on entity, annotation ignored: {}", propertyHolder.getEntityName()
      );
    }

    // try to find class level generators
    HashMap<String, IdGenerator> classGenerators = buildLocalGenerators( clazzToProcess, mappings );

    // check properties
    final InheritanceState.ElementsToProcess elementsToProcess = inheritanceState.getElementsToProcess();
    inheritanceState.postProcess( persistentClass, entityBinder );

    final boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE
        && inheritanceState.hasParents();
    Set<String> idPropertiesIfIdClass = new HashSet<String>();
    boolean isIdClass = mapAsIdClass(
        inheritanceStatePerClass,
        inheritanceState,
        persistentClass,
        entityBinder,
        propertyHolder,
        elementsToProcess,
        idPropertiesIfIdClass,
        mappings
    );

    if (!isIdClass) {
      entityBinder.setWrapIdsInEmbeddedComponents( elementsToProcess.getIdPropertyCount() > 1 );
    }

    processIdPropertiesIfNotAlready(
        inheritanceStatePerClass,
        mappings,
        persistentClass,
        entityBinder,
        propertyHolder,
        classGenerators,
        elementsToProcess,
        subclassAndSingleTableStrategy,
        idPropertiesIfIdClass
    );

    if ( !inheritanceState.hasParents() ) {
      final RootClass rootClass = (RootClass) persistentClass;
      mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
    }
    else {
      superEntity.addSubclass( (Subclass) persistentClass );
    }

    mappings.addClass( persistentClass );

    //Process secondary tables and complementary definitions (ie o.h.a.Table)
    mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder, clazzToProcess ) );

    //add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Table.class ) );
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Tables.class ) );

  }
View Full Code Here

    PersistentClass persistentClass = makePersistentClass( inheritanceState, superEntity );
    Entity entityAnn = clazzToProcess.getAnnotation( Entity.class );
    org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation(
        org.hibernate.annotations.Entity.class
    );
    EntityBinder entityBinder = new EntityBinder(
        entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
    );
    entityBinder.setInheritanceState( inheritanceState );

    bindQueries( clazzToProcess, mappings );
    bindFilterDefs( clazzToProcess, mappings );
    bindTypeDefs( clazzToProcess, mappings );
    bindFetchProfiles( clazzToProcess, mappings );
    BinderHelper.bindAnyMetaDefs( clazzToProcess, mappings );

    String schema = "";
    String table = ""; //might be no @Table annotation on the annotated class
    String catalog = "";
    List<UniqueConstraintHolder> uniqueConstraints = new ArrayList<UniqueConstraintHolder>();
    if ( clazzToProcess.isAnnotationPresent( javax.persistence.Table.class ) ) {
      javax.persistence.Table tabAnn = clazzToProcess.getAnnotation( javax.persistence.Table.class );
      table = tabAnn.name();
      schema = tabAnn.schema();
      catalog = tabAnn.catalog();
      uniqueConstraints = TableBinder.buildUniqueConstraintHolders( tabAnn.uniqueConstraints() );
    }

    Ejb3JoinColumn[] inheritanceJoinedColumns = makeInheritanceJoinColumns(
        clazzToProcess, mappings, inheritanceState, superEntity
    );
    Ejb3DiscriminatorColumn discriminatorColumn = null;
    if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      discriminatorColumn = processDiscriminatorProperties(
          clazzToProcess, mappings, inheritanceState, entityBinder
      );
    }

    entityBinder.setProxy( clazzToProcess.getAnnotation( Proxy.class ) );
    entityBinder.setBatchSize( clazzToProcess.getAnnotation( BatchSize.class ) );
    entityBinder.setWhere( clazzToProcess.getAnnotation( Where.class ) );
      entityBinder.setCache( determineCacheSettings( clazzToProcess, mappings ) );
      entityBinder.setNaturalIdCache( clazzToProcess, clazzToProcess.getAnnotation( NaturalIdCache.class ) );

    //Filters are not allowed on subclasses
    if ( !inheritanceState.hasParents() ) {
      bindFilters( clazzToProcess, entityBinder, mappings );
    }

    entityBinder.bindEntity();

    if ( inheritanceState.hasTable() ) {
      Check checkAnn = clazzToProcess.getAnnotation( Check.class );
      String constraints = checkAnn == null ?
          null :
          checkAnn.constraints();
      entityBinder.bindTable(
          schema, catalog, table, uniqueConstraints,
          constraints, inheritanceState.hasDenormalizedTable() ?
              superEntity.getTable() :
              null
      );
    }
    else if ( clazzToProcess.isAnnotationPresent( Table.class ) ) {
      LOG.invalidTableAnnotation( clazzToProcess.getName() );
    }


    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(
        clazzToProcess,
        persistentClass,
        entityBinder, mappings, inheritanceStatePerClass
    );

    javax.persistence.SecondaryTable secTabAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTable.class
    );
    javax.persistence.SecondaryTables secTabsAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTables.class
    );
    entityBinder.firstLevelSecondaryTablesBinding( secTabAnn, secTabsAnn );

    OnDelete onDeleteAnn = clazzToProcess.getAnnotation( OnDelete.class );
    boolean onDeleteAppropriate = false;
    if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) && inheritanceState.hasParents() ) {
      onDeleteAppropriate = true;
      final JoinedSubclass jsc = ( JoinedSubclass ) persistentClass;
      SimpleValue key = new DependantValue( mappings, jsc.getTable(), jsc.getIdentifier() );
      jsc.setKey( key );
      ForeignKey fk = clazzToProcess.getAnnotation( ForeignKey.class );
      if ( fk != null && !BinderHelper.isEmptyAnnotationValue( fk.name() ) ) {
        key.setForeignKeyName( fk.name() );
      }
      if ( onDeleteAnn != null ) {
        key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
      }
      else {
        key.setCascadeDeleteEnabled( false );
      }
      //we are never in a second pass at that stage, so queue it
      SecondPass sp = new JoinedSubclassFkSecondPass( jsc, inheritanceJoinedColumns, key, mappings );
      mappings.addSecondPass( sp );
      mappings.addSecondPass( new CreateKeySecondPass( jsc ) );

    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      if ( ! inheritanceState.hasParents() ) {
        if ( inheritanceState.hasSiblings() || !discriminatorColumn.isImplicit() ) {
          //need a discriminator column
          bindDiscriminatorToPersistentClass(
              ( RootClass ) persistentClass,
              discriminatorColumn,
              entityBinder.getSecondaryTables(),
              propertyHolder,
              mappings
          );
          entityBinder.bindDiscriminatorValue();//bind it again since the type might have changed
        }
      }
    }
    else if ( InheritanceType.TABLE_PER_CLASS.equals( inheritanceState.getType() ) ) {
      //nothing to do
    }
        if (onDeleteAnn != null && !onDeleteAppropriate) LOG.invalidOnDeleteAnnotation(propertyHolder.getEntityName());

    // try to find class level generators
    HashMap<String, IdGenerator> classGenerators = buildLocalGenerators( clazzToProcess, mappings );

    // check properties
    final InheritanceState.ElementsToProcess elementsToProcess = inheritanceState.getElementsToProcess();
    inheritanceState.postProcess( persistentClass, entityBinder );

    final boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE
        && inheritanceState.hasParents();
    Set<String> idPropertiesIfIdClass = new HashSet<String>();
    boolean isIdClass = mapAsIdClass(
        inheritanceStatePerClass,
        inheritanceState,
        persistentClass,
        entityBinder,
        propertyHolder,
        elementsToProcess,
        idPropertiesIfIdClass,
        mappings
    );

    if ( !isIdClass ) {
      entityBinder.setWrapIdsInEmbeddedComponents( elementsToProcess.getIdPropertyCount() > 1 );
    }

    processIdPropertiesIfNotAlready(
        inheritanceStatePerClass,
        mappings,
        persistentClass,
        entityBinder,
        propertyHolder,
        classGenerators,
        elementsToProcess,
        subclassAndSingleTableStrategy,
        idPropertiesIfIdClass
    );

    if ( !inheritanceState.hasParents() ) {
      final RootClass rootClass = ( RootClass ) persistentClass;
      mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
    }
    else {
      superEntity.addSubclass( ( Subclass ) persistentClass );
    }

    mappings.addClass( persistentClass );

    //Process secondary tables and complementary definitions (ie o.h.a.Table)
    mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder, clazzToProcess ) );

    //add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Table.class ) );
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Tables.class ) );

  }
View Full Code Here

        org.hibernate.annotations.Entity.class
    );
    org.hibernate.annotations.Cache cacheAnn = annotatedClass.getAnnotation(
        org.hibernate.annotations.Cache.class
    );
    EntityBinder entityBinder = new EntityBinder(
        entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
    );
    entityBinder.setDiscriminatorValue( discrimValue );
    entityBinder.setBatchSize( sizeAnn );
    entityBinder.setProxy( proxyAnn );
    entityBinder.setWhere( whereAnn );
    entityBinder.setCache( cacheAnn );
    entityBinder.setInheritanceState( inheritanceState );
    Filter filterAnn = annotatedClass.getAnnotation( Filter.class );
    if ( filterAnn != null ) {
      entityBinder.addFilter( filterAnn.name(), filterAnn.condition() );
    }
    Filters filtersAnn = annotatedClass.getAnnotation( Filters.class );
    if ( filtersAnn != null ) {
      for ( Filter filter : filtersAnn.value() ) {
        entityBinder.addFilter( filter.name(), filter.condition() );
      }
    }
    entityBinder.bindEntity();

    if ( inheritanceState.hasTable() ) {
      Check checkAnn = annotatedClass.getAnnotation( Check.class );
      String constraints = checkAnn == null ? null : checkAnn.constraints();
      entityBinder.bindTable(
          schema, catalog, table, uniqueConstraints,
          constraints, inheritanceState.hasDenormalizedTable() ? superEntity.getTable() : null
      );
    }
    else {
      if ( annotatedClass.isAnnotationPresent( Table.class ) ) {
        log.warn( "Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: " + clazzToProcess.getName() );
      }
    }
//    Map<String, Column[]> columnOverride = PropertyHolderBuilder.buildHierarchyColumnOverride(
//        clazzToProcess,
//        persistentClass.getClassName()
//    );
    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(
        clazzToProcess,
        persistentClass,
        entityBinder, mappings
    );

    javax.persistence.SecondaryTable secTabAnn = annotatedClass.getAnnotation(
        javax.persistence.SecondaryTable.class
    );
    javax.persistence.SecondaryTables secTabsAnn = annotatedClass.getAnnotation(
        javax.persistence.SecondaryTables.class
    );
    entityBinder.firstLevelSecondaryTablesBinding( secTabAnn, secTabsAnn );

    OnDelete onDeleteAnn = annotatedClass.getAnnotation( OnDelete.class );
    boolean onDeleteAppropriate = false;
    if ( InheritanceType.JOINED.equals( inheritanceState.type ) && inheritanceState.hasParents ) {
      onDeleteAppropriate = true;
      final JoinedSubclass jsc = (JoinedSubclass) persistentClass;
      if ( persistentClass.getEntityPersisterClass() == null ) {
        persistentClass.getRootClass().setEntityPersisterClass( JoinedSubclassEntityPersister.class );
      }
      SimpleValue key = new DependantValue( jsc.getTable(), jsc.getIdentifier() );
      jsc.setKey( key );
      if ( onDeleteAnn != null ) {
        key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
      }
      else {
        key.setCascadeDeleteEnabled( false );
      }
      TableBinder.bindFk( jsc.getSuperclass(), jsc, inheritanceJoinedColumns, key, false, mappings );
      //no need to handle inSecondPass this is an Etntiy related work
      mappings.addSecondPass( new CreateKeySecondPass( jsc ) );

    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.type ) ) {
      if ( inheritanceState.hasParents ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( SingleTableEntityPersister.class );
        }
      }
      else {
        if ( inheritanceState.hasSons || ! discriminatorColumn.isImplicit() ) {
          //need a discriminator column
          bindDiscriminatorToPersistentClass(
              (RootClass) persistentClass,
              discriminatorColumn,
              entityBinder.getSecondaryTables(),
              propertyHolder
          );
          entityBinder.bindDiscriminatorValue();//bind it again since the type might have changed
        }
      }
    }
    else if ( InheritanceType.TABLE_PER_CLASS.equals( inheritanceState.type ) ) {
      if ( inheritanceState.hasParents ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( UnionSubclassEntityPersister.class );
        }
      }
    }
    if ( onDeleteAnn != null && ! onDeleteAppropriate ) {
      log.warn(
          "Inapropriate use of @OnDelete on entity, annotation ignored: " + propertyHolder.getEntityName()
      );
    }

    //try to find class level generators
    HashMap<String, IdGenerator> classGenerators = buildLocalGenerators( annotatedClass, mappings );

    // check properties
    List<PropertyData> elements =
        getElementsToProcess(
            clazzToProcess, inheritanceStatePerClass, propertyHolder, entityBinder, mappings
        );
    if ( elements == null ) {
      throw new AnnotationException( "No identifier specified for entity: " + propertyHolder.getEntityName() );
    }
    final boolean subclassAndSingleTableStrategy = inheritanceState.type == InheritanceType.SINGLE_TABLE
        && inheritanceState.hasParents;
    //process idclass if any
    Set<String> idProperties = new HashSet<String>();
    IdClass idClass = null;
    if ( ! inheritanceState.hasParents ) {
      //look for idClass
      XClass current = inheritanceState.clazz;
      InheritanceState state = inheritanceState;
      do {
        current = state.clazz;
        if ( current.isAnnotationPresent( IdClass.class ) ) {
          idClass = current.getAnnotation( IdClass.class );
          break;
        }
        state = InheritanceState.getSuperclassInheritanceState(
            current, inheritanceStatePerClass, mappings.getReflectionManager()
        );
      }
      while ( state != null );
    }
    if ( idClass != null ) {
      XClass compositeClass = mappings.getReflectionManager().toXClass( idClass.value() );
      boolean isComponent = true;
      boolean propertyAnnotated = entityBinder.isPropertyAnnotated( compositeClass );
      String propertyAccessor = entityBinder.getPropertyAccessor( compositeClass );
      String generatorType = "assigned";
      String generator = BinderHelper.ANNOTATION_STRING_DEFAULT;
      PropertyData inferredData = new PropertyPreloadedData(
          entityBinder.getPropertyAccessor(), "id", compositeClass
      );
      HashMap<String, IdGenerator> localGenerators = new HashMap<String, IdGenerator>();
      boolean ignoreIdAnnotations = entityBinder.isIgnoreIdAnnotations();
      entityBinder.setIgnoreIdAnnotations( true );
      bindId(
          generatorType,
          generator,
          inferredData,
          null,
          propertyHolder,
          localGenerators,
          isComponent,
          propertyAnnotated,
          propertyAccessor, entityBinder,
          null,
          true,
          false, mappings
      );
      inferredData = new PropertyPreloadedData(
          propertyAccessor, "_identifierMapper", compositeClass
      );
      Component mapper = fillComponent(
          propertyHolder,
          inferredData,
          propertyAnnotated,
          propertyAccessor, false,
          entityBinder,
          true, true,
          false, mappings
      );
      entityBinder.setIgnoreIdAnnotations( ignoreIdAnnotations );
      persistentClass.setIdentifierMapper( mapper );
      Property property = new Property();
      property.setName( "_identifierMapper" );
      property.setNodeName( "id" );
      property.setUpdateable( false );
      property.setInsertable( false );
      property.setValue( mapper );
      property.setPropertyAccessorName( "embedded" );
      persistentClass.addProperty( property );
      entityBinder.setIgnoreIdAnnotations( true );

      Iterator properties = mapper.getPropertyIterator();
      while ( properties.hasNext() ) {
        idProperties.add( ( (Property) properties.next() ).getName() );
      }
    }
    Set<String> missingIdProperties = new HashSet<String>( idProperties );
    for ( PropertyData propertyAnnotatedElement : elements ) {
      String propertyName = propertyAnnotatedElement.getPropertyName();
      if ( ! idProperties.contains( propertyName ) ) {
        processElementAnnotations(
            propertyHolder,
            subclassAndSingleTableStrategy ? Nullability.FORCED_NULL : Nullability.NO_CONSTRAINT,
            propertyAnnotatedElement.getProperty(),
            propertyAnnotatedElement, classGenerators, entityBinder,
            false, false, false, mappings
        );
      }
      else {
        missingIdProperties.remove( propertyName );
      }
    }

    if ( missingIdProperties.size() != 0 ) {
      StringBuilder missings = new StringBuilder();
      for ( String property : missingIdProperties ) {
        missings.append( property ).append( ", " );
      }
      throw new AnnotationException(
          "Unable to find properties ("
              + missings.substring( 0, missings.length() - 2 )
              + ") in entity annotated with @IdClass:" + persistentClass.getEntityName()
      );
    }

    if ( ! inheritanceState.hasParents ) {
      final RootClass rootClass = (RootClass) persistentClass;
      //no need to handle inSecondPass this is an Etntiy related work
      mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
    }
    else {
      superEntity.addSubclass( (Subclass) persistentClass );
    }

    mappings.addClass( persistentClass );
    entityBinder.finalSecondaryTableBinding( propertyHolder );

    //add indexes
    entityBinder.addIndexes( annotatedClass.getAnnotation( org.hibernate.annotations.Table.class ) );
    entityBinder.addIndexes( annotatedClass.getAnnotation( org.hibernate.annotations.Tables.class ) );

  }
View Full Code Here

        org.hibernate.annotations.Entity.class
    );

    Cache cacheAnn = determineCacheSettings( clazzToProcess, mappings );

    EntityBinder entityBinder = new EntityBinder(
        entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
    );
    entityBinder.setDiscriminatorValue( discrimValue );
    entityBinder.setBatchSize( sizeAnn );
    entityBinder.setProxy( proxyAnn );
    entityBinder.setWhere( whereAnn );
    entityBinder.setCache( cacheAnn );
    entityBinder.setInheritanceState( inheritanceState );

    //Filters are not allowed on subclasses
    if ( !inheritanceState.hasParents() ) {
      bindFilters( clazzToProcess, entityBinder, mappings );
    }

    entityBinder.bindEntity();

    if ( inheritanceState.hasTable() ) {
      Check checkAnn = clazzToProcess.getAnnotation( Check.class );
      String constraints = checkAnn == null ?
          null :
          checkAnn.constraints();
      entityBinder.bindTable(
          schema, catalog, table, uniqueConstraints,
          constraints, inheritanceState.hasDenormalizedTable() ?
              superEntity.getTable() :
              null
      );
    }
    else {
      if ( clazzToProcess.isAnnotationPresent( Table.class ) ) {
        log.warn(
            "Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: " + clazzToProcess
                .getName()
        );
      }
    }

    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(
        clazzToProcess,
        persistentClass,
        entityBinder, mappings, inheritanceStatePerClass
    );

    javax.persistence.SecondaryTable secTabAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTable.class
    );
    javax.persistence.SecondaryTables secTabsAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTables.class
    );
    entityBinder.firstLevelSecondaryTablesBinding( secTabAnn, secTabsAnn );

    OnDelete onDeleteAnn = clazzToProcess.getAnnotation( OnDelete.class );
    boolean onDeleteAppropriate = false;
    if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) && inheritanceState.hasParents() ) {
      onDeleteAppropriate = true;
      final JoinedSubclass jsc = ( JoinedSubclass ) persistentClass;
      if ( persistentClass.getEntityPersisterClass() == null ) {
        persistentClass.getRootClass().setEntityPersisterClass( JoinedSubclassEntityPersister.class );
      }
      SimpleValue key = new DependantValue( mappings, jsc.getTable(), jsc.getIdentifier() );
      jsc.setKey( key );
      ForeignKey fk = clazzToProcess.getAnnotation( ForeignKey.class );
      if ( fk != null && !BinderHelper.isDefault( fk.name() ) ) {
        key.setForeignKeyName( fk.name() );
      }
      if ( onDeleteAnn != null ) {
        key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
      }
      else {
        key.setCascadeDeleteEnabled( false );
      }
      //we are never in a second pass at that stage, so queue it
      SecondPass sp = new JoinedSubclassFkSecondPass( jsc, inheritanceJoinedColumns, key, mappings );
      mappings.addSecondPass( sp );
      mappings.addSecondPass( new CreateKeySecondPass( jsc ) );

    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      if ( inheritanceState.hasParents() ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( SingleTableEntityPersister.class );
        }
      }
      else {
        if ( inheritanceState.hasSiblings() || !discriminatorColumn.isImplicit() ) {
          //need a discriminator column
          bindDiscriminatorToPersistentClass(
              ( RootClass ) persistentClass,
              discriminatorColumn,
              entityBinder.getSecondaryTables(),
              propertyHolder,
              mappings
          );
          entityBinder.bindDiscriminatorValue();//bind it again since the type might have changed
        }
      }
    }
    else if ( InheritanceType.TABLE_PER_CLASS.equals( inheritanceState.getType() ) ) {
      if ( inheritanceState.hasParents() ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( UnionSubclassEntityPersister.class );
        }
      }
    }
    if ( onDeleteAnn != null && !onDeleteAppropriate ) {
      log.warn(
          "Inapropriate use of @OnDelete on entity, annotation ignored: {}", propertyHolder.getEntityName()
      );
    }

    // try to find class level generators
    HashMap<String, IdGenerator> classGenerators = buildLocalGenerators( clazzToProcess, mappings );

    // check properties
    final InheritanceState.ElementsToProcess elementsToProcess = inheritanceState.getElementsToProcess();
    inheritanceState.postProcess( persistentClass, entityBinder );

    final boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE
        && inheritanceState.hasParents();
    Set<String> idPropertiesIfIdClass = new HashSet<String>();
    boolean isIdClass = mapAsIdClass(
        inheritanceStatePerClass,
        inheritanceState,
        persistentClass,
        entityBinder,
        propertyHolder,
        elementsToProcess,
        idPropertiesIfIdClass,
        mappings
    );

    if ( !isIdClass ) {
      entityBinder.setWrapIdsInEmbeddedComponents( elementsToProcess.getIdPropertyCount() > 1 );
    }

    processIdPropertiesIfNotAlready(
        inheritanceStatePerClass,
        mappings,
        persistentClass,
        entityBinder,
        propertyHolder,
        classGenerators,
        elementsToProcess,
        subclassAndSingleTableStrategy,
        idPropertiesIfIdClass
    );

    if ( !inheritanceState.hasParents() ) {
      final RootClass rootClass = ( RootClass ) persistentClass;
      mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
    }
    else {
      superEntity.addSubclass( ( Subclass ) persistentClass );
    }

    mappings.addClass( persistentClass );

    //Process secondary tables and complementary definitions (ie o.h.a.Table)
    mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder, clazzToProcess ) );

    //add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Table.class ) );
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Tables.class ) );

  }
View Full Code Here

    PersistentClass persistentClass = makePersistentClass( inheritanceState, superEntity );
    Entity entityAnn = clazzToProcess.getAnnotation( Entity.class );
    org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation(
        org.hibernate.annotations.Entity.class
    );
    EntityBinder entityBinder = new EntityBinder(
        entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
    );
    entityBinder.setInheritanceState( inheritanceState );

    bindQueries( clazzToProcess, mappings );
    bindFilterDefs( clazzToProcess, mappings );
    bindTypeDefs( clazzToProcess, mappings );
    bindFetchProfiles( clazzToProcess, mappings );
    BinderHelper.bindAnyMetaDefs( clazzToProcess, mappings );

    String schema = "";
    String table = ""; //might be no @Table annotation on the annotated class
    String catalog = "";
    List<UniqueConstraintHolder> uniqueConstraints = new ArrayList<UniqueConstraintHolder>();
    if ( clazzToProcess.isAnnotationPresent( javax.persistence.Table.class ) ) {
      javax.persistence.Table tabAnn = clazzToProcess.getAnnotation( javax.persistence.Table.class );
      table = tabAnn.name();
      schema = tabAnn.schema();
      catalog = tabAnn.catalog();
      uniqueConstraints = TableBinder.buildUniqueConstraintHolders( tabAnn.uniqueConstraints() );
    }

    Ejb3JoinColumn[] inheritanceJoinedColumns = makeInheritanceJoinColumns(
        clazzToProcess, mappings, inheritanceState, superEntity
    );
    Ejb3DiscriminatorColumn discriminatorColumn = null;
    if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      discriminatorColumn = processDiscriminatorProperties(
          clazzToProcess, mappings, inheritanceState, entityBinder
      );
    }

    entityBinder.setProxy( clazzToProcess.getAnnotation( Proxy.class ) );
    entityBinder.setBatchSize( clazzToProcess.getAnnotation( BatchSize.class ) );
    entityBinder.setWhere( clazzToProcess.getAnnotation( Where.class ) );
      entityBinder.setCache( determineCacheSettings( clazzToProcess, mappings ) );
      entityBinder.setNaturalIdCache( clazzToProcess, clazzToProcess.getAnnotation( NaturalIdCache.class ) );

    bindFilters( clazzToProcess, entityBinder, mappings );

    entityBinder.bindEntity();

    if ( inheritanceState.hasTable() ) {
      Check checkAnn = clazzToProcess.getAnnotation( Check.class );
      String constraints = checkAnn == null ?
          null :
          checkAnn.constraints();
      entityBinder.bindTable(
          schema, catalog, table, uniqueConstraints,
          constraints, inheritanceState.hasDenormalizedTable() ?
              superEntity.getTable() :
              null
      );
    }
    else if ( clazzToProcess.isAnnotationPresent( Table.class ) ) {
      LOG.invalidTableAnnotation( clazzToProcess.getName() );
    }


    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(
        clazzToProcess,
        persistentClass,
        entityBinder, mappings, inheritanceStatePerClass
    );

    javax.persistence.SecondaryTable secTabAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTable.class
    );
    javax.persistence.SecondaryTables secTabsAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTables.class
    );
    entityBinder.firstLevelSecondaryTablesBinding( secTabAnn, secTabsAnn );

    OnDelete onDeleteAnn = clazzToProcess.getAnnotation( OnDelete.class );
    boolean onDeleteAppropriate = false;
    if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) && inheritanceState.hasParents() ) {
      onDeleteAppropriate = true;
      final JoinedSubclass jsc = ( JoinedSubclass ) persistentClass;
      SimpleValue key = new DependantValue( mappings, jsc.getTable(), jsc.getIdentifier() );
      jsc.setKey( key );
      ForeignKey fk = clazzToProcess.getAnnotation( ForeignKey.class );
      if ( fk != null && !BinderHelper.isEmptyAnnotationValue( fk.name() ) ) {
        key.setForeignKeyName( fk.name() );
      }
      if ( onDeleteAnn != null ) {
        key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
      }
      else {
        key.setCascadeDeleteEnabled( false );
      }
      //we are never in a second pass at that stage, so queue it
      SecondPass sp = new JoinedSubclassFkSecondPass( jsc, inheritanceJoinedColumns, key, mappings );
      mappings.addSecondPass( sp );
      mappings.addSecondPass( new CreateKeySecondPass( jsc ) );

    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      if ( ! inheritanceState.hasParents() ) {
        if ( inheritanceState.hasSiblings() || !discriminatorColumn.isImplicit() ) {
          //need a discriminator column
          bindDiscriminatorToPersistentClass(
              ( RootClass ) persistentClass,
              discriminatorColumn,
              entityBinder.getSecondaryTables(),
              propertyHolder,
              mappings
          );
          entityBinder.bindDiscriminatorValue();//bind it again since the type might have changed
        }
      }
    }
    else if ( InheritanceType.TABLE_PER_CLASS.equals( inheritanceState.getType() ) ) {
      //nothing to do
    }
        if (onDeleteAnn != null && !onDeleteAppropriate) LOG.invalidOnDeleteAnnotation(propertyHolder.getEntityName());

    // try to find class level generators
    HashMap<String, IdGenerator> classGenerators = buildLocalGenerators( clazzToProcess, mappings );

    // check properties
    final InheritanceState.ElementsToProcess elementsToProcess = inheritanceState.getElementsToProcess();
    inheritanceState.postProcess( persistentClass, entityBinder );

    final boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE
        && inheritanceState.hasParents();
    Set<String> idPropertiesIfIdClass = new HashSet<String>();
    boolean isIdClass = mapAsIdClass(
        inheritanceStatePerClass,
        inheritanceState,
        persistentClass,
        entityBinder,
        propertyHolder,
        elementsToProcess,
        idPropertiesIfIdClass,
        mappings
    );

    if ( !isIdClass ) {
      entityBinder.setWrapIdsInEmbeddedComponents( elementsToProcess.getIdPropertyCount() > 1 );
    }

    processIdPropertiesIfNotAlready(
        inheritanceStatePerClass,
        mappings,
        persistentClass,
        entityBinder,
        propertyHolder,
        classGenerators,
        elementsToProcess,
        subclassAndSingleTableStrategy,
        idPropertiesIfIdClass
    );

    if ( !inheritanceState.hasParents() ) {
      final RootClass rootClass = ( RootClass ) persistentClass;
      mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
    }
    else {
      superEntity.addSubclass( ( Subclass ) persistentClass );
    }

    mappings.addClass( persistentClass );

    //Process secondary tables and complementary definitions (ie o.h.a.Table)
    mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder, clazzToProcess ) );

    //add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Table.class ) );
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Tables.class ) );

  }
View Full Code Here

    PersistentClass persistentClass = makePersistentClass( inheritanceState, superEntity );
    Entity entityAnn = clazzToProcess.getAnnotation( Entity.class );
    org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation(
        org.hibernate.annotations.Entity.class
    );
    EntityBinder entityBinder = new EntityBinder(
        entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
    );
    entityBinder.setInheritanceState( inheritanceState );

    bindQueries( clazzToProcess, mappings );
    bindFilterDefs( clazzToProcess, mappings );
    bindTypeDefs( clazzToProcess, mappings );
    bindFetchProfiles( clazzToProcess, mappings );
    BinderHelper.bindAnyMetaDefs( clazzToProcess, mappings );

    String schema = "";
    String table = ""; //might be no @Table annotation on the annotated class
    String catalog = "";
    List<UniqueConstraintHolder> uniqueConstraints = new ArrayList<UniqueConstraintHolder>();
    if ( clazzToProcess.isAnnotationPresent( javax.persistence.Table.class ) ) {
      javax.persistence.Table tabAnn = clazzToProcess.getAnnotation( javax.persistence.Table.class );
      table = tabAnn.name();
      schema = tabAnn.schema();
      catalog = tabAnn.catalog();
      uniqueConstraints = TableBinder.buildUniqueConstraintHolders( tabAnn.uniqueConstraints() );
    }

    Ejb3JoinColumn[] inheritanceJoinedColumns = makeInheritanceJoinColumns(
        clazzToProcess, mappings, inheritanceState, superEntity
    );
    Ejb3DiscriminatorColumn discriminatorColumn = null;
    if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      discriminatorColumn = processDiscriminatorProperties(
          clazzToProcess, mappings, inheritanceState, entityBinder
      );
    }

    entityBinder.setProxy( clazzToProcess.getAnnotation( Proxy.class ) );
    entityBinder.setBatchSize( clazzToProcess.getAnnotation( BatchSize.class ) );
    entityBinder.setWhere( clazzToProcess.getAnnotation( Where.class ) );
      entityBinder.setCache( determineCacheSettings( clazzToProcess, mappings ) );

    //Filters are not allowed on subclasses
    if ( !inheritanceState.hasParents() ) {
      bindFilters( clazzToProcess, entityBinder, mappings );
    }

    entityBinder.bindEntity();

    if ( inheritanceState.hasTable() ) {
      Check checkAnn = clazzToProcess.getAnnotation( Check.class );
      String constraints = checkAnn == null ?
          null :
          checkAnn.constraints();
      entityBinder.bindTable(
          schema, catalog, table, uniqueConstraints,
          constraints, inheritanceState.hasDenormalizedTable() ?
              superEntity.getTable() :
              null
      );
    }
    else {
      if ( clazzToProcess.isAnnotationPresent( Table.class ) ) {
        log.warn(
            "Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: " + clazzToProcess
                .getName()
        );
      }
    }

    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(
        clazzToProcess,
        persistentClass,
        entityBinder, mappings, inheritanceStatePerClass
    );

    javax.persistence.SecondaryTable secTabAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTable.class
    );
    javax.persistence.SecondaryTables secTabsAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTables.class
    );
    entityBinder.firstLevelSecondaryTablesBinding( secTabAnn, secTabsAnn );

    OnDelete onDeleteAnn = clazzToProcess.getAnnotation( OnDelete.class );
    boolean onDeleteAppropriate = false;
    if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) && inheritanceState.hasParents() ) {
      onDeleteAppropriate = true;
      final JoinedSubclass jsc = ( JoinedSubclass ) persistentClass;
      if ( persistentClass.getEntityPersisterClass() == null ) {
        persistentClass.getRootClass().setEntityPersisterClass( JoinedSubclassEntityPersister.class );
      }
      SimpleValue key = new DependantValue( mappings, jsc.getTable(), jsc.getIdentifier() );
      jsc.setKey( key );
      ForeignKey fk = clazzToProcess.getAnnotation( ForeignKey.class );
      if ( fk != null && !BinderHelper.isEmptyAnnotationValue( fk.name() ) ) {
        key.setForeignKeyName( fk.name() );
      }
      if ( onDeleteAnn != null ) {
        key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
      }
      else {
        key.setCascadeDeleteEnabled( false );
      }
      //we are never in a second pass at that stage, so queue it
      SecondPass sp = new JoinedSubclassFkSecondPass( jsc, inheritanceJoinedColumns, key, mappings );
      mappings.addSecondPass( sp );
      mappings.addSecondPass( new CreateKeySecondPass( jsc ) );

    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      if ( inheritanceState.hasParents() ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( SingleTableEntityPersister.class );
        }
      }
      else {
        if ( inheritanceState.hasSiblings() || !discriminatorColumn.isImplicit() ) {
          //need a discriminator column
          bindDiscriminatorToPersistentClass(
              ( RootClass ) persistentClass,
              discriminatorColumn,
              entityBinder.getSecondaryTables(),
              propertyHolder,
              mappings
          );
          entityBinder.bindDiscriminatorValue();//bind it again since the type might have changed
        }
      }
    }
    else if ( InheritanceType.TABLE_PER_CLASS.equals( inheritanceState.getType() ) ) {
      if ( inheritanceState.hasParents() ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( UnionSubclassEntityPersister.class );
        }
      }
    }
    if ( onDeleteAnn != null && !onDeleteAppropriate ) {
      log.warn(
          "Inapropriate use of @OnDelete on entity, annotation ignored: {}", propertyHolder.getEntityName()
      );
    }

    // try to find class level generators
    HashMap<String, IdGenerator> classGenerators = buildLocalGenerators( clazzToProcess, mappings );

    // check properties
    final InheritanceState.ElementsToProcess elementsToProcess = inheritanceState.getElementsToProcess();
    inheritanceState.postProcess( persistentClass, entityBinder );

    final boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE
        && inheritanceState.hasParents();
    Set<String> idPropertiesIfIdClass = new HashSet<String>();
    boolean isIdClass = mapAsIdClass(
        inheritanceStatePerClass,
        inheritanceState,
        persistentClass,
        entityBinder,
        propertyHolder,
        elementsToProcess,
        idPropertiesIfIdClass,
        mappings
    );

    if ( !isIdClass ) {
      entityBinder.setWrapIdsInEmbeddedComponents( elementsToProcess.getIdPropertyCount() > 1 );
    }

    processIdPropertiesIfNotAlready(
        inheritanceStatePerClass,
        mappings,
        persistentClass,
        entityBinder,
        propertyHolder,
        classGenerators,
        elementsToProcess,
        subclassAndSingleTableStrategy,
        idPropertiesIfIdClass
    );

    if ( !inheritanceState.hasParents() ) {
      final RootClass rootClass = ( RootClass ) persistentClass;
      mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
    }
    else {
      superEntity.addSubclass( ( Subclass ) persistentClass );
    }

    mappings.addClass( persistentClass );

    //Process secondary tables and complementary definitions (ie o.h.a.Table)
    mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder, clazzToProcess ) );

    //add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Table.class ) );
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Tables.class ) );

  }
View Full Code Here

    PersistentClass persistentClass = makePersistentClass( inheritanceState, superEntity );
    Entity entityAnn = clazzToProcess.getAnnotation( Entity.class );
    org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation(
        org.hibernate.annotations.Entity.class
    );
    EntityBinder entityBinder = new EntityBinder(
        entityAnn, hibEntityAnn, clazzToProcess, persistentClass, mappings
    );
    entityBinder.setInheritanceState( inheritanceState );

    bindQueries( clazzToProcess, mappings );
    bindFilterDefs( clazzToProcess, mappings );
    bindTypeDefs( clazzToProcess, mappings );
    bindFetchProfiles( clazzToProcess, mappings );
    BinderHelper.bindAnyMetaDefs( clazzToProcess, mappings );

    String schema = "";
    String table = ""; //might be no @Table annotation on the annotated class
    String catalog = "";
    List<UniqueConstraintHolder> uniqueConstraints = new ArrayList<UniqueConstraintHolder>();
    if ( clazzToProcess.isAnnotationPresent( javax.persistence.Table.class ) ) {
      javax.persistence.Table tabAnn = clazzToProcess.getAnnotation( javax.persistence.Table.class );
      table = tabAnn.name();
      schema = tabAnn.schema();
      catalog = tabAnn.catalog();
      uniqueConstraints = TableBinder.buildUniqueConstraintHolders( tabAnn.uniqueConstraints() );
    }

    Ejb3JoinColumn[] inheritanceJoinedColumns = makeInheritanceJoinColumns(
        clazzToProcess, mappings, inheritanceState, superEntity
    );
    Ejb3DiscriminatorColumn discriminatorColumn = null;
    if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      discriminatorColumn = processDiscriminatorProperties(
          clazzToProcess, mappings, inheritanceState, entityBinder
      );
    }

    entityBinder.setProxy( clazzToProcess.getAnnotation( Proxy.class ) );
    entityBinder.setBatchSize( clazzToProcess.getAnnotation( BatchSize.class ) );
    entityBinder.setWhere( clazzToProcess.getAnnotation( Where.class ) );
      entityBinder.setCache( determineCacheSettings( clazzToProcess, mappings ) );

    //Filters are not allowed on subclasses
    if ( !inheritanceState.hasParents() ) {
      bindFilters( clazzToProcess, entityBinder, mappings );
    }

    entityBinder.bindEntity();

    if ( inheritanceState.hasTable() ) {
      Check checkAnn = clazzToProcess.getAnnotation( Check.class );
      String constraints = checkAnn == null ?
          null :
          checkAnn.constraints();
      entityBinder.bindTable(
          schema, catalog, table, uniqueConstraints,
          constraints, inheritanceState.hasDenormalizedTable() ?
              superEntity.getTable() :
              null
      );
    }
    else {
      if ( clazzToProcess.isAnnotationPresent( Table.class ) ) {
        log.warn(
            "Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: " + clazzToProcess
                .getName()
        );
      }
    }

    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(
        clazzToProcess,
        persistentClass,
        entityBinder, mappings, inheritanceStatePerClass
    );

    javax.persistence.SecondaryTable secTabAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTable.class
    );
    javax.persistence.SecondaryTables secTabsAnn = clazzToProcess.getAnnotation(
        javax.persistence.SecondaryTables.class
    );
    entityBinder.firstLevelSecondaryTablesBinding( secTabAnn, secTabsAnn );

    OnDelete onDeleteAnn = clazzToProcess.getAnnotation( OnDelete.class );
    boolean onDeleteAppropriate = false;
    if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) && inheritanceState.hasParents() ) {
      onDeleteAppropriate = true;
      final JoinedSubclass jsc = ( JoinedSubclass ) persistentClass;
      if ( persistentClass.getEntityPersisterClass() == null ) {
        persistentClass.getRootClass().setEntityPersisterClass( JoinedSubclassEntityPersister.class );
      }
      SimpleValue key = new DependantValue( mappings, jsc.getTable(), jsc.getIdentifier() );
      jsc.setKey( key );
      ForeignKey fk = clazzToProcess.getAnnotation( ForeignKey.class );
      if ( fk != null && !BinderHelper.isEmptyAnnotationValue( fk.name() ) ) {
        key.setForeignKeyName( fk.name() );
      }
      if ( onDeleteAnn != null ) {
        key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
      }
      else {
        key.setCascadeDeleteEnabled( false );
      }
      //we are never in a second pass at that stage, so queue it
      SecondPass sp = new JoinedSubclassFkSecondPass( jsc, inheritanceJoinedColumns, key, mappings );
      mappings.addSecondPass( sp );
      mappings.addSecondPass( new CreateKeySecondPass( jsc ) );

    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
      if ( inheritanceState.hasParents() ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( SingleTableEntityPersister.class );
        }
      }
      else {
        if ( inheritanceState.hasSiblings() || !discriminatorColumn.isImplicit() ) {
          //need a discriminator column
          bindDiscriminatorToPersistentClass(
              ( RootClass ) persistentClass,
              discriminatorColumn,
              entityBinder.getSecondaryTables(),
              propertyHolder,
              mappings
          );
          entityBinder.bindDiscriminatorValue();//bind it again since the type might have changed
        }
      }
    }
    else if ( InheritanceType.TABLE_PER_CLASS.equals( inheritanceState.getType() ) ) {
      if ( inheritanceState.hasParents() ) {
        if ( persistentClass.getEntityPersisterClass() == null ) {
          persistentClass.getRootClass().setEntityPersisterClass( UnionSubclassEntityPersister.class );
        }
      }
    }
    if ( onDeleteAnn != null && !onDeleteAppropriate ) {
      log.warn(
          "Inapropriate use of @OnDelete on entity, annotation ignored: {}", propertyHolder.getEntityName()
      );
    }

    // try to find class level generators
    HashMap<String, IdGenerator> classGenerators = buildLocalGenerators( clazzToProcess, mappings );

    // check properties
    final InheritanceState.ElementsToProcess elementsToProcess = inheritanceState.getElementsToProcess();
    inheritanceState.postProcess( persistentClass, entityBinder );

    final boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE
        && inheritanceState.hasParents();
    Set<String> idPropertiesIfIdClass = new HashSet<String>();
    boolean isIdClass = mapAsIdClass(
        inheritanceStatePerClass,
        inheritanceState,
        persistentClass,
        entityBinder,
        propertyHolder,
        elementsToProcess,
        idPropertiesIfIdClass,
        mappings
    );

    if ( !isIdClass ) {
      entityBinder.setWrapIdsInEmbeddedComponents( elementsToProcess.getIdPropertyCount() > 1 );
    }

    processIdPropertiesIfNotAlready(
        inheritanceStatePerClass,
        mappings,
        persistentClass,
        entityBinder,
        propertyHolder,
        classGenerators,
        elementsToProcess,
        subclassAndSingleTableStrategy,
        idPropertiesIfIdClass
    );

    if ( !inheritanceState.hasParents() ) {
      final RootClass rootClass = ( RootClass ) persistentClass;
      mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
    }
    else {
      superEntity.addSubclass( ( Subclass ) persistentClass );
    }

    mappings.addClass( persistentClass );

    //Process secondary tables and complementary definitions (ie o.h.a.Table)
    mappings.addSecondPass( new SecondaryTableSecondPass( entityBinder, propertyHolder, clazzToProcess ) );

    //add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Table.class ) );
    entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Tables.class ) );

  }
View Full Code Here

TOP

Related Classes of org.hibernate.cfg.annotations.EntityBinder

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.