Package javax.persistence

Examples of javax.persistence.Column


    private static String getColumnName(Field field) {
        if (field == null) {
            return null;
        }

        Column colSpec = field.getAnnotation(Column.class);

        if (colSpec != null && !colSpec.name().isEmpty()) {
            return colSpec.name();
        } else {
            return null;
        }
    }
View Full Code Here


    private static String getColumnName(AnnotatedField field) {
        if (field == null) {
            return null;
        }

        Column colSpec = field.getAnnotation(Column.class);

        if (colSpec != null && !colSpec.name().isEmpty()) {
            return colSpec.name().toUpperCase();
        } else {
            return null;
        }
    }
View Full Code Here

    Class<T> theType = cfMapDef.getClazz();
    // do {
    Field[] fieldArr = theType.getDeclaredFields();
    for (Field f : fieldArr) {
      Column colAnno = f.getAnnotation(javax.persistence.Column.class);
      if (null != colAnno) {
        PropertyDescriptor pd = pdMap.get(f.getName());
        if (null == pd) {
          throw new IllegalStateException("Property, " + cfMapDef.getClazz().getSimpleName() + "."
              + f.getName() + ", does not have proper setter/getter");
        }

        @SuppressWarnings("unchecked")
        PropertyMappingDefinition md =
          new PropertyMappingDefinition(pd, colAnno.name(), DefaultConverter.class);
        cfMapDef.addPropertyDefinition(md);
      }

      // TODO lookup JPA 2 spec for class-level ids
      Id idAnno = f.getAnnotation(Id.class);
View Full Code Here

  public void testColumnNoAttributes() throws Exception {
    reader = getReader( Entity3.class, "field1", "element-collection.orm16.xml" );
    assertAnnotationPresent( ElementCollection.class );
    assertAnnotationPresent( Column.class );
    Column column = reader.getAnnotation( Column.class );
    assertEquals( "", column.name() );
    assertFalse( column.unique() );
    assertTrue( column.nullable() );
    assertTrue( column.insertable() );
    assertTrue( column.updatable() );
    assertEquals( "", column.columnDefinition() );
    assertEquals( "", column.table() );
    assertEquals( 255, column.length() );
    assertEquals( 0, column.precision() );
    assertEquals( 0, column.scale() );
  }
View Full Code Here

  public void testColumnAllAttributes() throws Exception {
    reader = getReader( Entity3.class, "field1", "element-collection.orm17.xml" );
    assertAnnotationPresent( ElementCollection.class );
    assertAnnotationPresent( Column.class );
    Column column = reader.getAnnotation( Column.class );
    assertEquals( "col1", column.name() );
    assertTrue( column.unique() );
    assertFalse( column.nullable() );
    assertFalse( column.insertable() );
    assertFalse( column.updatable() );
    assertEquals( "int", column.columnDefinition() );
    assertEquals( "table1", column.table() );
    assertEquals( 50, column.length() );
    assertEquals( 2, column.precision() );
    assertEquals( 1, column.scale() );
  }
View Full Code Here

      // Test added to avoid default value (which defaults name to "")
      if (graph.name() != null && graph.name().trim().length() > 0)
        return graph.name();
    }
    if (p.getAnnotation(Column.class) != null) {
      Column column = p.getAnnotation(Column.class);
      if (column.name() != null && column.name().trim().length() > 0)
        return column.name();
    }
    return getDefaultEdgeNameFor(p);
  }
View Full Code Here

      // Test added to avoid default value (which defaults name to "")
      if (graph.name() != null && graph.name().trim().length() > 0)
        return graph.name();
    }
    if (p.getAnnotation(Column.class) != null) {
      Column column = p.getAnnotation(Column.class);
      if (column.name() != null && column.name().trim().length() > 0)
        return column.name();
    }
    return getDefaultEdgeNameFor(p);
  }
View Full Code Here

     */
    static final List<java.lang.reflect.Field> getAnnotatedMembers(Class<?> type, String name) {
        List<java.lang.reflect.Field> result = new ArrayList<java.lang.reflect.Field>();

        for (java.lang.reflect.Field member : getInstanceMembers(type)) {
            Column annotation = member.getAnnotation(Column.class);

            if (annotation != null) {
                if (name.equals(annotation.name())) {
                    result.add(member);
                }
            }
        }

View Full Code Here

    /**
     * Get the first getter method annotated with a given column name
     */
    static final Method getAnnotatedGetter(Class<?> type, String name) {
        for (Method method : getInstanceMethods(type)) {
            Column annotation = method.getAnnotation(Column.class);

            if (annotation != null && name.equals(annotation.name())) {

                // Annotated getter
                if (method.getParameterTypes().length == 0) {
                    return method;
                }
View Full Code Here

            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 ) ) };
          }
          else if ( property.isAnnotationPresent( org.hibernate.annotations.MapKey.class ) ) {
            if ( isJPA2 == null ) {
              isJPA2 = Boolean.FALSE;
            }
            keyColumns = property.getAnnotation( org.hibernate.annotations.MapKey.class ).columns();
          }

          //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
                    )
                )
            };
          }
          else if ( property.isAnnotationPresent( org.hibernate.annotations.MapKeyManyToMany.class ) ) {
            if ( isJPA2 == null ) {
              isJPA2 = Boolean.FALSE;
            }
            joinKeyColumns = property.getAnnotation( org.hibernate.annotations.MapKeyManyToMany.class )
                .joinColumns();
          }

          //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
            || collectionOfElementsAnn != null //Hibernate legacy
            ) {
          for ( Ejb3JoinColumn column : joinColumns ) {
            if ( column.isSecondary() ) {
              throw new NotYetImplementedException( "Collections having FK in secondary table" );
            }
          }
          collectionBinder.setFkJoinColumns( joinColumns );
          mappedBy = "";
          final Class<?> targetElement = elementCollectionAnn != null ?
              elementCollectionAnn.targetClass() :
              collectionOfElementsAnn.targetElement();
          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 ) {
            final PropertyData mapsIdProperty = BinderHelper.getPropertyOverriddenByMapperOrMapsId(
                isId, propertyHolder, property.getName(), mappings
            );
            referencedEntityName = mapsIdProperty.getClassOrElementName();
          }
          AccessType propertyAccessor = entityBinder.getPropertyAccessor( property );
          propertyBinder = bindComponent(
              inferredData,
              propertyHolder,
              propertyAccessor,
              entityBinder,
              isIdentifierMapper,
              mappings,
              isComponentEmbedded,
              isId,
              inheritanceStatePerClass,
              referencedEntityName,
              isOverridden ? ( Ejb3JoinColumn[] ) columns : null
          );
        }
        else {
          //provide the basic property mapping
          boolean optional = true;
          boolean lazy = false;
          if ( property.isAnnotationPresent( Basic.class ) ) {
            Basic ann = property.getAnnotation( Basic.class );
            optional = ann.optional();
            lazy = ann.fetch() == FetchType.LAZY;
          }
          //implicit type will check basic types and Serializable classes
          if ( isId || ( !optional && nullability != Nullability.FORCED_NULL ) ) {
            //force columns to not null
            for ( Ejb3Column col : columns ) {
View Full Code Here

TOP

Related Classes of javax.persistence.Column

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.