Package org.hibernate.annotations.common.reflection

Examples of org.hibernate.annotations.common.reflection.XClass


     * and we create an identifier mapper containing the id properties of the main entity
     *
     * In JPA 2, there is a shortcut if the id class is the Pk of the associated class pointed to by the id
     * it ought to be treated as an embedded and not a real IdClass (at least in the Hibernate's internal way
     */
    XClass classWithIdClass = inheritanceState.getClassWithIdClass( false );
    if ( classWithIdClass != null ) {
      IdClass idClass = classWithIdClass.getAnnotation( IdClass.class );
      XClass compositeClass = mappings.getReflectionManager().toXClass( idClass.value() );
      PropertyData inferredData = new PropertyPreloadedData(
          entityBinder.getPropertyAccessType(), "id", compositeClass
      );
      PropertyData baseInferredData = new PropertyPreloadedData(
          entityBinder.getPropertyAccessType(), "id", classWithIdClass
View Full Code Here


      );
      final InheritanceState state = inheritanceStatePerClass.get( idPropertyOnBaseClass.getClassOrElement() );
      if ( state == null ) {
        return false; //while it is likely a user error, let's consider it is something that might happen
      }
      final XClass associatedClassWithIdClass = state.getClassWithIdClass( true );
      if ( associatedClassWithIdClass == null ) {
        //we cannot know for sure here unless we try and find the @EmbeddedId
        //Let's not do this thorough checking but do some extra validation
        final XProperty property = idPropertyOnBaseClass.getProperty();
        return property.isAnnotationPresent( ManyToOne.class )
            || property.isAnnotationPresent( OneToOne.class );

      }
      else {
        final XClass idClass = mappings.getReflectionManager().toXClass(
            associatedClassWithIdClass.getAnnotation( IdClass.class ).value()
        );
        return idClass.equals( compositeClass );
      }
    }
    else {
      return false;
    }
View Full Code Here

  private static void bindFilters(XClass annotatedClass, EntityBinder entityBinder,
                  Mappings mappings) {

    bindFilters( annotatedClass, entityBinder );

    XClass classToProcess = annotatedClass.getSuperclass();
    while ( classToProcess != null ) {
      AnnotatedClassType classType = mappings.getClassType( classToProcess );
      if ( AnnotatedClassType.EMBEDDABLE_SUPERCLASS.equals( classType ) ) {
        bindFilters( classToProcess, entityBinder );
      }
      classToProcess = classToProcess.getSuperclass();
    }

  }
View Full Code Here

      PropertyContainer propertyContainer,
      XProperty property,
      List<PropertyData> annElts,
      String propertyAccessor,
      Mappings mappings) {
    final XClass declaringClass = propertyContainer.getDeclaringClass();
    final XClass entity = propertyContainer.getEntityAtStake();
    int idPropertyCounter = 0;
    PropertyData propertyAnnotatedElement = new PropertyInferredData(
        declaringClass, property, propertyAccessor,
        mappings.getReflectionManager()
    );
View Full Code Here

        propertyHolder, nullability, property, inferredData, entityBinder, mappings
    ).extractMetadata();
    Ejb3Column[] columns = columnsBuilder.getColumns();
    Ejb3JoinColumn[] joinColumns = columnsBuilder.getJoinColumns();

    final XClass returnedClass = inferredData.getClassOrElement();

    //prepare PropertyBinder
    PropertyBinder propertyBinder = new PropertyBinder();
    propertyBinder.setName( inferredData.getPropertyName() );
    propertyBinder.setReturnedClassName( inferredData.getTypeName() );
    propertyBinder.setAccessType( inferredData.getDefaultAccess() );
    propertyBinder.setHolder( propertyHolder );
    propertyBinder.setProperty( property );
    propertyBinder.setReturnedClass( inferredData.getPropertyClass() );
    propertyBinder.setMappings( mappings );
    if ( isIdentifierMapper ) {
      propertyBinder.setInsertable( false );
      propertyBinder.setUpdatable( false );
    }
    propertyBinder.setDeclaringClass( inferredData.getDeclaringClass() );
    propertyBinder.setEntityBinder( entityBinder );
    propertyBinder.setInheritanceStatePerClass( inheritanceStatePerClass );

    boolean isId = !entityBinder.isIgnoreIdAnnotations() &&
        ( property.isAnnotationPresent( Id.class )
            || property.isAnnotationPresent( EmbeddedId.class ) );
    propertyBinder.setId( isId );

    if ( property.isAnnotationPresent( Version.class ) ) {
      if ( isIdentifierMapper ) {
        throw new AnnotationException(
            "@IdClass class should not have @Version property"
        );
      }
      if ( !( propertyHolder.getPersistentClass() instanceof RootClass ) ) {
        throw new AnnotationException(
            "Unable to define/override @Version on a subclass: "
                + propertyHolder.getEntityName()
        );
      }
      if ( !propertyHolder.isEntity() ) {
        throw new AnnotationException(
            "Unable to define @Version on an embedded class: "
                + propertyHolder.getEntityName()
        );
      }
      if ( traceEnabled ) {
        LOG.tracev( "{0} is a version property", inferredData.getPropertyName() );
      }
      RootClass rootClass = ( RootClass ) propertyHolder.getPersistentClass();
      propertyBinder.setColumns( columns );
      Property prop = propertyBinder.makePropertyValueAndBind();
      setVersionInformation( property, propertyBinder );
      rootClass.setVersion( prop );

      //If version is on a mapped superclass, update the mapping
      final org.hibernate.mapping.MappedSuperclass superclass = BinderHelper.getMappedSuperclassOrNull(
          inferredData.getDeclaringClass(),
          inheritanceStatePerClass,
          mappings
      );
      if ( superclass != null ) {
        superclass.setDeclaredVersion( prop );
      }
      else {
        //we know the property is on the actual entity
        rootClass.setDeclaredVersion( prop );
      }

      SimpleValue simpleValue = ( SimpleValue ) prop.getValue();
      simpleValue.setNullValue( "undefined" );
      rootClass.setOptimisticLockStyle( OptimisticLockStyle.VERSION );
      if ( traceEnabled ) {
        LOG.tracev( "Version name: {0}, unsavedValue: {1}", rootClass.getVersion().getName(),
            ( (SimpleValue) rootClass.getVersion().getValue() ).getNullValue() );
      }
    }
    else {
      final boolean forcePersist = property.isAnnotationPresent( MapsId.class )
          || property.isAnnotationPresent( Id.class );
      if ( property.isAnnotationPresent( ManyToOne.class ) ) {
        ManyToOne ann = property.getAnnotation( ManyToOne.class );

        //check validity
        if ( property.isAnnotationPresent( Column.class )
            || property.isAnnotationPresent( Columns.class ) ) {
          throw new AnnotationException(
              "@Column(s) not allowed on a @ManyToOne property: "
                  + BinderHelper.getPath( propertyHolder, inferredData )
          );
        }

        Cascade hibernateCascade = property.getAnnotation( Cascade.class );
        NotFound notFound = property.getAnnotation( NotFound.class );
        boolean ignoreNotFound = notFound != null && notFound.action().equals( NotFoundAction.IGNORE );
        OnDelete onDeleteAnn = property.getAnnotation( OnDelete.class );
        boolean onDeleteCascade = onDeleteAnn != null && OnDeleteAction.CASCADE.equals( onDeleteAnn.action() );
        JoinTable assocTable = propertyHolder.getJoinTable( property );
        if ( assocTable != null ) {
          Join join = propertyHolder.addJoin( assocTable, false );
          for ( Ejb3JoinColumn joinColumn : joinColumns ) {
            joinColumn.setExplicitTableName( join.getTable().getName() );
          }
        }
        final boolean mandatory = !ann.optional() || forcePersist;
        bindManyToOne(
            getCascadeStrategy( ann.cascade(), hibernateCascade, false, forcePersist ),
            joinColumns,
            !mandatory,
            ignoreNotFound, onDeleteCascade,
            ToOneBinder.getTargetEntity( inferredData, mappings ),
            propertyHolder,
            inferredData, false, isIdentifierMapper,
            inSecondPass, propertyBinder, mappings
        );
      }
      else if ( property.isAnnotationPresent( OneToOne.class ) ) {
        OneToOne ann = property.getAnnotation( OneToOne.class );

        //check validity
        if ( property.isAnnotationPresent( Column.class )
            || property.isAnnotationPresent( Columns.class ) ) {
          throw new AnnotationException(
              "@Column(s) not allowed on a @OneToOne property: "
                  + BinderHelper.getPath( propertyHolder, inferredData )
          );
        }

        //FIXME support a proper PKJCs
        boolean trueOneToOne = property.isAnnotationPresent( PrimaryKeyJoinColumn.class )
            || property.isAnnotationPresent( PrimaryKeyJoinColumns.class );
        Cascade hibernateCascade = property.getAnnotation( Cascade.class );
        NotFound notFound = property.getAnnotation( NotFound.class );
        boolean ignoreNotFound = notFound != null && notFound.action().equals( NotFoundAction.IGNORE );
        OnDelete onDeleteAnn = property.getAnnotation( OnDelete.class );
        boolean onDeleteCascade = onDeleteAnn != null && OnDeleteAction.CASCADE.equals( onDeleteAnn.action() );
        JoinTable assocTable = propertyHolder.getJoinTable( property );
        if ( assocTable != null ) {
          Join join = propertyHolder.addJoin( assocTable, false );
          for ( Ejb3JoinColumn joinColumn : joinColumns ) {
            joinColumn.setExplicitTableName( join.getTable().getName() );
          }
        }
        //MapsId means the columns belong to the pk => not null
        //@OneToOne with @PKJC can still be optional
        final boolean mandatory = !ann.optional() || forcePersist;
        bindOneToOne(
            getCascadeStrategy( ann.cascade(), hibernateCascade, ann.orphanRemoval(), forcePersist ),
            joinColumns,
            !mandatory,
            getFetchMode( ann.fetch() ),
            ignoreNotFound, onDeleteCascade,
            ToOneBinder.getTargetEntity( inferredData, mappings ),
            propertyHolder,
            inferredData,
            ann.mappedBy(),
            trueOneToOne,
            isIdentifierMapper,
            inSecondPass,
            propertyBinder,
            mappings
        );
      }
      else if ( property.isAnnotationPresent( org.hibernate.annotations.Any.class ) ) {

        //check validity
        if ( property.isAnnotationPresent( Column.class )
            || property.isAnnotationPresent( Columns.class ) ) {
          throw new AnnotationException(
              "@Column(s) not allowed on a @Any property: "
                  + BinderHelper.getPath( propertyHolder, inferredData )
          );
        }

        Cascade hibernateCascade = property.getAnnotation( Cascade.class );
        OnDelete onDeleteAnn = property.getAnnotation( OnDelete.class );
        boolean onDeleteCascade = onDeleteAnn != null && OnDeleteAction.CASCADE.equals( onDeleteAnn.action() );
        JoinTable assocTable = propertyHolder.getJoinTable( property );
        if ( assocTable != null ) {
          Join join = propertyHolder.addJoin( assocTable, false );
          for ( Ejb3JoinColumn joinColumn : joinColumns ) {
            joinColumn.setExplicitTableName( join.getTable().getName() );
          }
        }
        bindAny(
            getCascadeStrategy( null, hibernateCascade, false, forcePersist ),
            //@Any has not cascade attribute
            joinColumns,
            onDeleteCascade,
            nullability,
            propertyHolder,
            inferredData,
            entityBinder,
            isIdentifierMapper,
            mappings
        );
      }
      else if ( property.isAnnotationPresent( OneToMany.class )
          || property.isAnnotationPresent( ManyToMany.class )
          || property.isAnnotationPresent( ElementCollection.class )
          || property.isAnnotationPresent( ManyToAny.class ) ) {
        OneToMany oneToManyAnn = property.getAnnotation( OneToMany.class );
        ManyToMany manyToManyAnn = property.getAnnotation( ManyToMany.class );
        ElementCollection elementCollectionAnn = property.getAnnotation( ElementCollection.class );

        final IndexColumn indexColumn;

        if ( property.isAnnotationPresent( OrderColumn.class ) ) {
          indexColumn = IndexColumn.buildColumnFromAnnotation(
              property.getAnnotation( OrderColumn.class ),
              propertyHolder,
              inferredData,
              entityBinder.getSecondaryTables(),
              mappings
          );
          if ( property.isAnnotationPresent( ListIndexBase.class ) ) {
            indexColumn.setBase( ( property.getAnnotation( ListIndexBase.class ) ).value() );
          }
        }
        else {
          //if @IndexColumn is not there, the generated IndexColumn is an implicit column and not used.
          //so we can leave the legacy processing as the default
          indexColumn = IndexColumn.buildColumnFromAnnotation(
              property.getAnnotation( org.hibernate.annotations.IndexColumn.class ),
              propertyHolder,
              inferredData,
              mappings
          );
        }
        CollectionBinder collectionBinder = CollectionBinder.getCollectionBinder(
            propertyHolder.getEntityName(),
            property,
            !indexColumn.isImplicit(),
            property.isAnnotationPresent( MapKeyType.class ),
            mappings
        );
        collectionBinder.setIndexColumn( indexColumn );
        collectionBinder.setMapKey( property.getAnnotation( MapKey.class ) );
        collectionBinder.setPropertyName( inferredData.getPropertyName() );

        collectionBinder.setBatchSize( property.getAnnotation( BatchSize.class ) );

        collectionBinder.setJpaOrderBy( property.getAnnotation( javax.persistence.OrderBy.class ) );
        collectionBinder.setSqlOrderBy( property.getAnnotation( OrderBy.class ) );

        collectionBinder.setSort( property.getAnnotation( Sort.class ) );
        collectionBinder.setNaturalSort( property.getAnnotation( SortNatural.class ) );
        collectionBinder.setComparatorSort( property.getAnnotation( SortComparator.class ) );

        Cache cachAnn = property.getAnnotation( Cache.class );
        collectionBinder.setCache( cachAnn );
        collectionBinder.setPropertyHolder( propertyHolder );
        Cascade hibernateCascade = property.getAnnotation( Cascade.class );
        NotFound notFound = property.getAnnotation( NotFound.class );
        boolean ignoreNotFound = notFound != null && notFound.action().equals( NotFoundAction.IGNORE );
        collectionBinder.setIgnoreNotFound( ignoreNotFound );
        collectionBinder.setCollectionType( inferredData.getProperty().getElementClass() );
        collectionBinder.setMappings( mappings );
        collectionBinder.setAccessType( inferredData.getDefaultAccess() );

        Ejb3Column[] elementColumns;
        //do not use "element" if you are a JPA 2 @ElementCollection only for legacy Hibernate mappings
        boolean isJPA2ForValueMapping = property.isAnnotationPresent( ElementCollection.class );
        PropertyData virtualProperty = isJPA2ForValueMapping ? inferredData : new WrappedInferredData(
            inferredData, "element"
        );
        if ( property.isAnnotationPresent( Column.class ) || property.isAnnotationPresent(
            Formula.class
        ) ) {
          Column ann = property.getAnnotation( Column.class );
          Formula formulaAnn = property.getAnnotation( Formula.class );
          elementColumns = Ejb3Column.buildColumnFromAnnotation(
              new Column[] { ann },
              formulaAnn,
              nullability,
              propertyHolder,
              virtualProperty,
              entityBinder.getSecondaryTables(),
              mappings
          );
        }
        else if ( property.isAnnotationPresent( Columns.class ) ) {
          Columns anns = property.getAnnotation( Columns.class );
          elementColumns = Ejb3Column.buildColumnFromAnnotation(
              anns.columns(), null, nullability, propertyHolder, virtualProperty,
              entityBinder.getSecondaryTables(), mappings
          );
        }
        else {
          elementColumns = Ejb3Column.buildColumnFromAnnotation(
              null,
              null,
              nullability,
              propertyHolder,
              virtualProperty,
              entityBinder.getSecondaryTables(),
              mappings
          );
        }
        {
          Column[] keyColumns = null;
          //JPA 2 has priority and has different default column values, differenciate legacy from JPA 2
          Boolean isJPA2 = null;
          if ( property.isAnnotationPresent( MapKeyColumn.class ) ) {
            isJPA2 = Boolean.TRUE;
            keyColumns = new Column[] { new MapKeyColumnDelegator( property.getAnnotation( MapKeyColumn.class ) ) };
          }

          //not explicitly legacy
          if ( isJPA2 == null ) {
            isJPA2 = Boolean.TRUE;
          }

          //nullify empty array
          keyColumns = keyColumns != null && keyColumns.length > 0 ? keyColumns : null;

          //"mapkey" is the legacy column name of the key column pre JPA 2
          PropertyData mapKeyVirtualProperty = new WrappedInferredData( inferredData, "mapkey" );
          Ejb3Column[] mapColumns = Ejb3Column.buildColumnFromAnnotation(
              keyColumns,
              null,
              Nullability.FORCED_NOT_NULL,
              propertyHolder,
              isJPA2 ? inferredData : mapKeyVirtualProperty,
              isJPA2 ? "_KEY" : null,
              entityBinder.getSecondaryTables(),
              mappings
          );
          collectionBinder.setMapKeyColumns( mapColumns );
        }
        {
          JoinColumn[] joinKeyColumns = null;
          //JPA 2 has priority and has different default column values, differenciate legacy from JPA 2
          Boolean isJPA2 = null;
          if ( property.isAnnotationPresent( MapKeyJoinColumns.class ) ) {
            isJPA2 = Boolean.TRUE;
            final MapKeyJoinColumn[] mapKeyJoinColumns = property.getAnnotation( MapKeyJoinColumns.class )
                .value();
            joinKeyColumns = new JoinColumn[mapKeyJoinColumns.length];
            int index = 0;
            for ( MapKeyJoinColumn joinColumn : mapKeyJoinColumns ) {
              joinKeyColumns[index] = new MapKeyJoinColumnDelegator( joinColumn );
              index++;
            }
            if ( property.isAnnotationPresent( MapKeyJoinColumn.class ) ) {
              throw new AnnotationException(
                  "@MapKeyJoinColumn and @MapKeyJoinColumns used on the same property: "
                      + BinderHelper.getPath( propertyHolder, inferredData )
              );
            }
          }
          else if ( property.isAnnotationPresent( MapKeyJoinColumn.class ) ) {
            isJPA2 = Boolean.TRUE;
            joinKeyColumns = new JoinColumn[] {
                new MapKeyJoinColumnDelegator(
                    property.getAnnotation(
                        MapKeyJoinColumn.class
                    )
                )
            };
          }
          //not explicitly legacy
          if ( isJPA2 == null ) {
            isJPA2 = Boolean.TRUE;
          }

          PropertyData mapKeyVirtualProperty = new WrappedInferredData( inferredData, "mapkey" );
          Ejb3JoinColumn[] mapJoinColumns = Ejb3JoinColumn.buildJoinColumnsWithDefaultColumnSuffix(
              joinKeyColumns,
              null,
              entityBinder.getSecondaryTables(),
              propertyHolder,
              isJPA2 ? inferredData.getPropertyName() : mapKeyVirtualProperty.getPropertyName(),
              isJPA2 ? "_KEY" : null,
              mappings
          );
          collectionBinder.setMapKeyManyToManyColumns( mapJoinColumns );
        }

        //potential element
        collectionBinder.setEmbedded( property.isAnnotationPresent( Embedded.class ) );
        collectionBinder.setElementColumns( elementColumns );
        collectionBinder.setProperty( property );

        //TODO enhance exception with @ManyToAny and @CollectionOfElements
        if ( oneToManyAnn != null && manyToManyAnn != null ) {
          throw new AnnotationException(
              "@OneToMany and @ManyToMany on the same property is not allowed: "
                  + propertyHolder.getEntityName() + "." + inferredData.getPropertyName()
          );
        }
        String mappedBy = null;
        if ( oneToManyAnn != null ) {
          for ( Ejb3JoinColumn column : joinColumns ) {
            if ( column.isSecondary() ) {
              throw new NotYetImplementedException( "Collections having FK in secondary table" );
            }
          }
          collectionBinder.setFkJoinColumns( joinColumns );
          mappedBy = oneToManyAnn.mappedBy();
          collectionBinder.setTargetEntity(
              mappings.getReflectionManager().toXClass( oneToManyAnn.targetEntity() )
          );
          collectionBinder.setCascadeStrategy(
              getCascadeStrategy(
                  oneToManyAnn.cascade(), hibernateCascade, oneToManyAnn.orphanRemoval(), false
              )
          );
          collectionBinder.setOneToMany( true );
        }
        else if ( elementCollectionAnn != null ) {
          for ( Ejb3JoinColumn column : joinColumns ) {
            if ( column.isSecondary() ) {
              throw new NotYetImplementedException( "Collections having FK in secondary table" );
            }
          }
          collectionBinder.setFkJoinColumns( joinColumns );
          mappedBy = "";
          final Class<?> targetElement = elementCollectionAnn.targetClass();
          collectionBinder.setTargetEntity(
              mappings.getReflectionManager().toXClass( targetElement )
          );
          //collectionBinder.setCascadeStrategy( getCascadeStrategy( embeddedCollectionAnn.cascade(), hibernateCascade ) );
          collectionBinder.setOneToMany( true );
        }
        else if ( manyToManyAnn != null ) {
          mappedBy = manyToManyAnn.mappedBy();
          collectionBinder.setTargetEntity(
              mappings.getReflectionManager().toXClass( manyToManyAnn.targetEntity() )
          );
          collectionBinder.setCascadeStrategy(
              getCascadeStrategy(
                  manyToManyAnn.cascade(), hibernateCascade, false, false
              )
          );
          collectionBinder.setOneToMany( false );
        }
        else if ( property.isAnnotationPresent( ManyToAny.class ) ) {
          mappedBy = "";
          collectionBinder.setTargetEntity(
              mappings.getReflectionManager().toXClass( void.class )
          );
          collectionBinder.setCascadeStrategy( getCascadeStrategy( null, hibernateCascade, false, false ) );
          collectionBinder.setOneToMany( false );
        }
        collectionBinder.setMappedBy( mappedBy );

        bindJoinedTableAssociation(
            property, mappings, entityBinder, collectionBinder, propertyHolder, inferredData, mappedBy
        );

        OnDelete onDeleteAnn = property.getAnnotation( OnDelete.class );
        boolean onDeleteCascade = onDeleteAnn != null && OnDeleteAction.CASCADE.equals( onDeleteAnn.action() );
        collectionBinder.setCascadeDeleteEnabled( onDeleteCascade );
        if ( isIdentifierMapper ) {
          collectionBinder.setInsertable( false );
          collectionBinder.setUpdatable( false );
        }
        if ( property.isAnnotationPresent( CollectionId.class ) ) { //do not compute the generators unless necessary
          HashMap<String, IdGenerator> localGenerators = ( HashMap<String, IdGenerator> ) classGenerators.clone();
          localGenerators.putAll( buildLocalGenerators( property, mappings ) );
          collectionBinder.setLocalGenerators( localGenerators );

        }
        collectionBinder.setInheritanceStatePerClass( inheritanceStatePerClass );
        collectionBinder.setDeclaringClass( inferredData.getDeclaringClass() );
        collectionBinder.bind();

      }
      //Either a regular property or a basic @Id or @EmbeddedId while not ignoring id annotations
      else if ( !isId || !entityBinder.isIgnoreIdAnnotations() ) {
        //define whether the type is a component or not

        boolean isComponent = false;

        //Overrides from @MapsId if needed
        boolean isOverridden = false;
        if ( isId || propertyHolder.isOrWithinEmbeddedId() || propertyHolder.isInIdClass() ) {
          //the associated entity could be using an @IdClass making the overridden property a component
          final PropertyData overridingProperty = BinderHelper.getPropertyOverriddenByMapperOrMapsId(
              isId, propertyHolder, property.getName(), mappings
          );
          if ( overridingProperty != null ) {
            isOverridden = true;
            final InheritanceState state = inheritanceStatePerClass.get( overridingProperty.getClassOrElement() );
            if ( state != null ) {
              isComponent = isComponent || state.hasIdClassOrEmbeddedId();
            }
            //Get the new column
            columns = columnsBuilder.overrideColumnFromMapperOrMapsIdProperty( isId );
          }
        }

        isComponent = isComponent
            || property.isAnnotationPresent( Embedded.class )
            || property.isAnnotationPresent( EmbeddedId.class )
            || returnedClass.isAnnotationPresent( Embeddable.class );


        if ( isComponent ) {
          String referencedEntityName = null;
          if ( isOverridden ) {
View Full Code Here

      throw new AnnotationException(
          "@IdClass class should not have @Id nor @EmbeddedId properties: "
              + BinderHelper.getPath( propertyHolder, inferredData )
      );
    }
    XClass returnedClass = inferredData.getClassOrElement();
    XProperty property = inferredData.getProperty();
    //clone classGenerator and override with local values
    HashMap<String, IdGenerator> localGenerators = ( HashMap<String, IdGenerator> ) classGenerators.clone();
    localGenerators.putAll( buildLocalGenerators( property, mappings ) );

    //manage composite related metadata
    //guess if its a component and find id data access (property, field etc)
    final boolean isComponent = returnedClass.isAnnotationPresent( Embeddable.class )
        || property.isAnnotationPresent( EmbeddedId.class );

    GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class );
    String generatorType = generatedValue != null ?
        generatorType( generatedValue.strategy(), mappings ) :
View Full Code Here

    // propertyHolder here is the owner of the component property.  Tell it we are about to start the component...

    propertyHolder.startingProperty( inferredData.getProperty() );

    final XClass xClassProcessed = inferredData.getPropertyClass();
    List<PropertyData> classElements = new ArrayList<PropertyData>();
    XClass returnedClassOrElement = inferredData.getClassOrElement();

    List<PropertyData> baseClassElements = null;
    Map<String, PropertyData> orderedBaseClassElements = new HashMap<String, PropertyData>();
    XClass baseReturnedClassOrElement;
    if ( baseInferredData != null ) {
      baseClassElements = new ArrayList<PropertyData>();
      baseReturnedClassOrElement = baseInferredData.getClassOrElement();
      bindTypeDefs( baseReturnedClassOrElement, mappings );
      PropertyContainer propContainer = new PropertyContainer( baseReturnedClassOrElement, xClassProcessed );
      addElementsOfClass( baseClassElements, propertyAccessor, propContainer, mappings );
      for ( PropertyData element : baseClassElements ) {
        orderedBaseClassElements.put( element.getPropertyName(), element );
      }
    }

    //embeddable elements can have type defs
    bindTypeDefs( returnedClassOrElement, mappings );
    PropertyContainer propContainer = new PropertyContainer( returnedClassOrElement, xClassProcessed );
    addElementsOfClass( classElements, propertyAccessor, propContainer, mappings );

    //add elements of the embeddable superclass
    XClass superClass = xClassProcessed.getSuperclass();
    while ( superClass != null && superClass.isAnnotationPresent( MappedSuperclass.class ) ) {
      //FIXME: proper support of typevariables incl var resolved at upper levels
      propContainer = new PropertyContainer( superClass, xClassProcessed );
      addElementsOfClass( classElements, propertyAccessor, propContainer, mappings );
      superClass = superClass.getSuperclass();
    }
    if ( baseClassElements != null ) {
      //useful to avoid breaking pre JPA 2 mappings
      if ( !hasAnnotationsOnIdClass( xClassProcessed ) ) {
        for ( int i = 0; i < classElements.size(); i++ ) {
View Full Code Here

      PropertyData inferredData,
      PropertyData baseInferredData,
      AccessType propertyAccessor,
      Mappings mappings) {
    List<PropertyData> baseClassElements = new ArrayList<PropertyData>();
    XClass baseReturnedClassOrElement = baseInferredData.getClassOrElement();
    PropertyContainer propContainer = new PropertyContainer(
        baseReturnedClassOrElement, inferredData.getPropertyClass()
    );
    addElementsOfClass( baseClassElements, propertyAccessor, propContainer, mappings );
    //Id properties are on top and there is only one
View Full Code Here

      return targetEntity.getName();
    }
  }

  public static String getReferenceEntityName(PropertyData propertyData, Mappings mappings) {
    XClass targetEntity = getTargetEntity( propertyData, mappings );
    if ( AnnotationBinder.isDefault( targetEntity, mappings ) ) {
      return propertyData.getClassOrElementName();
    }
    else {
      return targetEntity.getName();
    }
  }
View Full Code Here

  public void setType(XProperty property, XClass returnedClass, String declaringClassName, AttributeConverterDefinition attributeConverterDefinition) {
    if ( returnedClass == null ) {
      // we cannot guess anything
      return;
    }
    XClass returnedClassOrElement = returnedClass;
                boolean isArray = false;
    if ( property.isArray() ) {
      returnedClassOrElement = property.getElementClass();
      isArray = true;
    }
    this.xproperty = property;
    Properties typeParameters = this.typeParameters;
    typeParameters.clear();
    String type = BinderHelper.ANNOTATION_STRING_DEFAULT;

    final boolean isNationalized = property.isAnnotationPresent( Nationalized.class )
        || mappings.useNationalizedCharacterData();

    Type annType = property.getAnnotation( Type.class );
    if ( annType != null ) {
      setExplicitType( annType );
      type = explicitType;
    }
    else if ( ( !key && property.isAnnotationPresent( Temporal.class ) )
        || ( key && property.isAnnotationPresent( MapKeyTemporal.class ) ) ) {

      boolean isDate;
      if ( mappings.getReflectionManager().equals( returnedClassOrElement, Date.class ) ) {
        isDate = true;
      }
      else if ( mappings.getReflectionManager().equals( returnedClassOrElement, Calendar.class ) ) {
        isDate = false;
      }
      else {
        throw new AnnotationException(
            "@Temporal should only be set on a java.util.Date or java.util.Calendar property: "
                + StringHelper.qualify( persistentClassName, propertyName )
        );
      }
      final TemporalType temporalType = getTemporalType( property );
      switch ( temporalType ) {
        case DATE:
          type = isDate ? "date" : "calendar_date";
          break;
        case TIME:
          type = "time";
          if ( !isDate ) {
            throw new NotYetImplementedException(
                "Calendar cannot persist TIME only"
                    + StringHelper.qualify( persistentClassName, propertyName )
            );
          }
          break;
        case TIMESTAMP:
          type = isDate ? "timestamp" : "calendar";
          break;
        default:
          throw new AssertionFailure( "Unknown temporal type: " + temporalType );
      }
      explicitType = type;
    }
    else if ( !key && property.isAnnotationPresent( Lob.class ) ) {
      if ( mappings.getReflectionManager().equals( returnedClassOrElement, java.sql.Clob.class ) ) {
        type = isNationalized
            ? StandardBasicTypes.NCLOB.getName()
            : StandardBasicTypes.CLOB.getName();
      }
      else if ( mappings.getReflectionManager().equals( returnedClassOrElement, java.sql.NClob.class ) ) {
        type = StandardBasicTypes.NCLOB.getName();
      }
      else if ( mappings.getReflectionManager().equals( returnedClassOrElement, java.sql.Blob.class ) ) {
        type = "blob";
      }
      else if ( mappings.getReflectionManager().equals( returnedClassOrElement, String.class ) ) {
        type = isNationalized
            ? StandardBasicTypes.MATERIALIZED_NCLOB.getName()
            : StandardBasicTypes.MATERIALIZED_CLOB.getName();
      }
      else if ( mappings.getReflectionManager().equals( returnedClassOrElement, Character.class ) && isArray ) {
        type = isNationalized
            ? CharacterArrayNClobType.class.getName()
            : CharacterArrayClobType.class.getName();
      }
      else if ( mappings.getReflectionManager().equals( returnedClassOrElement, char.class ) && isArray ) {
        type = isNationalized
            ? PrimitiveCharacterArrayNClobType.class.getName()
            : PrimitiveCharacterArrayClobType.class.getName();
      }
      else if ( mappings.getReflectionManager().equals( returnedClassOrElement, Byte.class ) && isArray ) {
        type = WrappedMaterializedBlobType.class.getName();
      }
      else if ( mappings.getReflectionManager().equals( returnedClassOrElement, byte.class ) && isArray ) {
        type = StandardBasicTypes.MATERIALIZED_BLOB.getName();
      }
      else if ( mappings.getReflectionManager()
          .toXClass( Serializable.class )
          .isAssignableFrom( returnedClassOrElement ) ) {
        type = SerializableToBlobType.class.getName();
        typeParameters.setProperty(
            SerializableToBlobType.CLASS_NAME,
            returnedClassOrElement.getName()
        );
      }
      else {
        type = "blob";
      }
      explicitType = type;
    }
    else if ( ( !key && property.isAnnotationPresent( Enumerated.class ) )
        || ( key && property.isAnnotationPresent( MapKeyEnumerated.class ) ) ) {
      final Class attributeJavaType = mappings.getReflectionManager().toClass( returnedClassOrElement );
      if ( !Enum.class.isAssignableFrom( attributeJavaType ) ) {
        throw new AnnotationException(
            String.format(
                "Attribute [%s.%s] was annotated as enumerated, but its java type is not an enum [%s]",
                declaringClassName,
                xproperty.getName(),
                attributeJavaType.getName()
            )
        );
      }
      type = EnumType.class.getName();
      explicitType = type;
    }
    else if ( isNationalized ) {
      if ( mappings.getReflectionManager().equals( returnedClassOrElement, String.class ) ) {
        // nvarchar
        type = StringNVarcharType.INSTANCE.getName();
        explicitType = type;
      }
      else if ( mappings.getReflectionManager().equals( returnedClassOrElement, Character.class ) ) {
        if ( isArray ) {
          // nvarchar
          type = StringNVarcharType.INSTANCE.getName();
        }
        else {
          // nchar
          type = CharacterNCharType.INSTANCE.getName();
        }
        explicitType = type;
      }
    }

    // implicit type will check basic types and Serializable classes
    if ( columns == null ) {
      throw new AssertionFailure( "SimpleValueBinder.setColumns should be set before SimpleValueBinder.setType" );
    }

    if ( BinderHelper.ANNOTATION_STRING_DEFAULT.equals( type ) ) {
      if ( returnedClassOrElement.isEnum() ) {
        type = EnumType.class.getName();
      }
    }

    defaultType = BinderHelper.isEmptyAnnotationValue( type ) ? returnedClassName : type;
View Full Code Here

TOP

Related Classes of org.hibernate.annotations.common.reflection.XClass

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.