Search in sources :

Example 1 with GeneratedValue

use of javax.persistence.GeneratedValue in project hibernate-orm by hibernate.

the class AnnotationBinder method processId.

private static void processId(PropertyHolder propertyHolder, PropertyData inferredData, SimpleValue idValue, HashMap<String, IdentifierGeneratorDefinition> classGenerators, boolean isIdentifierMapper, MetadataBuildingContext buildingContext) {
    if (isIdentifierMapper) {
        throw new AnnotationException("@IdClass class should not have @Id nor @EmbeddedId properties: " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    XClass entityXClass = inferredData.getClassOrElement();
    XProperty idXProperty = inferredData.getProperty();
    // clone classGenerator and override with local values
    // HashMap<String, IdentifierGeneratorDefinition> localGenerators = ( HashMap<String, IdentifierGeneratorDefinition> ) classGenerators.clone();
    // localGenerators.putAll( buildGenerators( idXProperty, buildingContext ) );
    buildGenerators(idXProperty, buildingContext);
    // manage composite related metadata
    // guess if its a component and find id data access (property, field etc)
    final boolean isComponent = entityXClass.isAnnotationPresent(Embeddable.class) || idXProperty.isAnnotationPresent(EmbeddedId.class);
    GeneratedValue generatedValue = idXProperty.getAnnotation(GeneratedValue.class);
    String generatorType = generatedValue != null ? generatorType(generatedValue, buildingContext, entityXClass) : "assigned";
    String generatorName = generatedValue != null ? generatedValue.generator() : BinderHelper.ANNOTATION_STRING_DEFAULT;
    if (isComponent) {
        // a component must not have any generator
        generatorType = "assigned";
    }
    SecondPass secondPass = new IdGeneratorResolverSecondPass(idValue, idXProperty, generatorType, generatorName, buildingContext);
    buildingContext.getMetadataCollector().addSecondPass(secondPass);
    if (LOG.isTraceEnabled()) {
        LOG.tracev("Bind {0} on {1}", (isComponent ? "@EmbeddedId" : "@Id"), inferredData.getPropertyName());
    }
}
Also used : GeneratedValue(javax.persistence.GeneratedValue) XProperty(org.hibernate.annotations.common.reflection.XProperty) AnnotationException(org.hibernate.AnnotationException) EmbeddedId(javax.persistence.EmbeddedId) XClass(org.hibernate.annotations.common.reflection.XClass) Embeddable(javax.persistence.Embeddable)

Example 2 with GeneratedValue

use of javax.persistence.GeneratedValue in project jirm by agentgt.

the class SqlParameterDefinition method parameterDef.

static SqlParameterDefinition parameterDef(SqlObjectConfig config, Class<?> objectType, String parameterName, Class<?> parameterType, int order) {
    final SqlParameterDefinition definition;
    String sn = null;
    ManyToOne manyToOne = getAnnotation(objectType, parameterName, ManyToOne.class);
    if (manyToOne != null) {
        Class<?> subK = checkNotNull(manyToOne.targetEntity(), "targetEntity not set");
        JoinColumn joinColumn = getAnnotation(objectType, parameterName, JoinColumn.class);
        SqlObjectDefinition<?> od = SqlObjectDefinition.fromClass(subK, config);
        checkState(!od.getIdParameters().isEmpty(), "No id parameters");
        if (joinColumn != null)
            sn = joinColumn.name();
        if (sn == null)
            sn = config.getNamingStrategy().propertyToColumnName(parameterName);
        FetchType fetch = manyToOne.fetch();
        int depth;
        if (FetchType.LAZY == fetch) {
            depth = 1;
        } else {
            depth = config.getMaximumLoadDepth();
        }
        SqlParameterObjectDefinition sod = new SqlParameterObjectDefinition(od, depth);
        definition = SqlParameterDefinition.newComplexInstance(config.getConverter(), parameterName, sod, order, sn);
    } else {
        Column col = getAnnotation(objectType, parameterName, Column.class);
        if (col != null && !isNullOrEmpty(col.name()))
            sn = col.name();
        Id id = getAnnotation(objectType, parameterName, Id.class);
        Version version = getAnnotation(objectType, parameterName, Version.class);
        GeneratedValue generated = getAnnotation(objectType, parameterName, GeneratedValue.class);
        Enumerated enumerated = getAnnotation(objectType, parameterName, Enumerated.class);
        boolean idFlag = id != null;
        boolean versionFlag = version != null;
        boolean generatedFlag = generated != null;
        if (sn == null)
            sn = config.getNamingStrategy().propertyToColumnName(parameterName);
        definition = SqlParameterDefinition.newSimpleInstance(config.getConverter(), parameterName, parameterType, order, sn, idFlag, versionFlag, generatedFlag, Optional.fromNullable(enumerated));
    }
    return definition;
}
Also used : FetchType(javax.persistence.FetchType) ManyToOne(javax.persistence.ManyToOne) GeneratedValue(javax.persistence.GeneratedValue) Enumerated(javax.persistence.Enumerated) JoinColumn(javax.persistence.JoinColumn) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) Version(javax.persistence.Version) Id(javax.persistence.Id)

Example 3 with GeneratedValue

use of javax.persistence.GeneratedValue in project cosmic by MissionCriticalCloud.

the class Attribute method setupColumnInfo.

protected void setupColumnInfo(final Class<?> clazz, final AttributeOverride[] overrides, final String tableName, final boolean isEmbedded, final boolean isId) {
    flags = Flag.Selectable.setTrue(flags);
    final GeneratedValue gv = field.getAnnotation(GeneratedValue.class);
    if (gv != null) {
        if (gv.strategy() == GenerationType.IDENTITY) {
            flags = Flag.DbGenerated.setTrue(flags);
        } else if (gv.strategy() == GenerationType.SEQUENCE) {
            assert (false) : "Sequence generation not supported.";
            flags = Flag.DaoGenerated.setTrue(flags);
            flags = Flag.Insertable.setTrue(flags);
            flags = Flag.SequenceGV.setTrue(flags);
        } else if (gv.strategy() == GenerationType.TABLE) {
            flags = Flag.DaoGenerated.setTrue(flags);
            flags = Flag.Insertable.setTrue(flags);
            flags = Flag.TableGV.setTrue(flags);
        } else if (gv.strategy() == GenerationType.AUTO) {
            flags = Flag.DaoGenerated.setTrue(flags);
            flags = Flag.Insertable.setTrue(flags);
            flags = Flag.AutoGV.setTrue(flags);
        }
    }
    if (isEmbedded) {
        flags = Flag.Embedded.setTrue(flags);
    }
    if (isId) {
        flags = Flag.Id.setTrue(flags);
    } else {
        final Id id = field.getAnnotation(Id.class);
        if (id != null) {
            flags = Flag.Id.setTrue(flags);
        }
    }
    column = field.getAnnotation(Column.class);
    if (gv == null) {
        if (column == null || (column.insertable() && column.table().length() == 0)) {
            flags = Flag.Insertable.setTrue(flags);
        }
        if (column == null || (column.updatable() && column.table().length() == 0)) {
            flags = Flag.Updatable.setTrue(flags);
        }
        if (column == null || column.nullable()) {
            flags = Flag.Nullable.setTrue(flags);
        }
        final Encrypt encrypt = field.getAnnotation(Encrypt.class);
        if (encrypt != null && encrypt.encrypt()) {
            flags = Flag.Encrypted.setTrue(flags);
        }
    }
    final ElementCollection ec = field.getAnnotation(ElementCollection.class);
    if (ec != null) {
        flags = Flag.Insertable.setFalse(flags);
        flags = Flag.Selectable.setFalse(flags);
    }
    final Temporal temporal = field.getAnnotation(Temporal.class);
    if (temporal != null) {
        if (temporal.value() == TemporalType.DATE) {
            flags = Flag.Date.setTrue(flags);
        } else if (temporal.value() == TemporalType.TIME) {
            flags = Flag.Time.setTrue(flags);
        } else if (temporal.value() == TemporalType.TIMESTAMP) {
            flags = Flag.TimeStamp.setTrue(flags);
        }
    }
    if (column != null && column.table().length() > 0) {
        table = column.table();
    }
    columnName = DbUtil.getColumnName(field, overrides);
}
Also used : GeneratedValue(javax.persistence.GeneratedValue) Temporal(javax.persistence.Temporal) Column(javax.persistence.Column) Id(javax.persistence.Id) ElementCollection(javax.persistence.ElementCollection)

Example 4 with GeneratedValue

use of javax.persistence.GeneratedValue in project dal by ctripcorp.

the class EntityManager method processAutoIncrementColumn.

private void processAutoIncrementColumn(Field field, String currentClassName, Set<String> classNameSet) {
    GeneratedValue generatedValue = field.getAnnotation(GeneratedValue.class);
    if (!autoIncremental && null != generatedValue && (generatedValue.strategy() == GenerationType.AUTO || generatedValue.strategy() == GenerationType.IDENTITY))
        autoIncremental = true;
    if (field.getAnnotation(Id.class) != null && generatedValue != null && generatedValue.strategy() == GenerationType.AUTO) {
        // first add identity(auto increment)
        if (classNameSet.isEmpty()) {
            classNameSet.add(currentClassName);
            identityList.add(field);
            return;
        }
        if (classNameSet.contains(currentClassName)) {
            identityList.add(field);
        }
    }
}
Also used : GeneratedValue(javax.persistence.GeneratedValue) Id(javax.persistence.Id)

Example 5 with GeneratedValue

use of javax.persistence.GeneratedValue in project CloudStack-archive by CloudStack-extras.

the class Attribute method setupColumnInfo.

protected void setupColumnInfo(Class<?> clazz, AttributeOverride[] overrides, String tableName, boolean isEmbedded, boolean isId) {
    flags = Flag.Selectable.setTrue(flags);
    GeneratedValue gv = field.getAnnotation(GeneratedValue.class);
    if (gv != null) {
        if (gv.strategy() == GenerationType.IDENTITY) {
            flags = Flag.DbGenerated.setTrue(flags);
        } else if (gv.strategy() == GenerationType.SEQUENCE) {
            assert (false) : "Sequence generation not supported.";
            flags = Flag.DaoGenerated.setTrue(flags);
            flags = Flag.Insertable.setTrue(flags);
            flags = Flag.SequenceGV.setTrue(flags);
        } else if (gv.strategy() == GenerationType.TABLE) {
            flags = Flag.DaoGenerated.setTrue(flags);
            flags = Flag.Insertable.setTrue(flags);
            flags = Flag.TableGV.setTrue(flags);
        } else if (gv.strategy() == GenerationType.AUTO) {
            flags = Flag.DaoGenerated.setTrue(flags);
            flags = Flag.Insertable.setTrue(flags);
            flags = Flag.AutoGV.setTrue(flags);
        }
    }
    if (isEmbedded) {
        flags = Flag.Embedded.setTrue(flags);
    }
    if (isId) {
        flags = Flag.Id.setTrue(flags);
    } else {
        Id id = field.getAnnotation(Id.class);
        if (id != null) {
            flags = Flag.Id.setTrue(flags);
        }
    }
    column = field.getAnnotation(Column.class);
    if (gv == null) {
        if (column == null || (column.insertable() && column.table().length() == 0)) {
            flags = Flag.Insertable.setTrue(flags);
        }
        if (column == null || (column.updatable() && column.table().length() == 0)) {
            flags = Flag.Updatable.setTrue(flags);
        }
        if (column == null || column.nullable()) {
            flags = Flag.Nullable.setTrue(flags);
        }
        if (column != null && column.encryptable()) {
            flags = Flag.Encrypted.setTrue(flags);
        }
    }
    ElementCollection ec = field.getAnnotation(ElementCollection.class);
    if (ec != null) {
        flags = Flag.Insertable.setFalse(flags);
        flags = Flag.Selectable.setFalse(flags);
    }
    Temporal temporal = field.getAnnotation(Temporal.class);
    if (temporal != null) {
        if (temporal.value() == TemporalType.DATE) {
            flags = Flag.Date.setTrue(flags);
        } else if (temporal.value() == TemporalType.TIME) {
            flags = Flag.Time.setTrue(flags);
        } else if (temporal.value() == TemporalType.TIMESTAMP) {
            flags = Flag.TimeStamp.setTrue(flags);
        }
    }
    if (column != null && column.table().length() > 0) {
        table = column.table();
    }
    columnName = DbUtil.getColumnName(field, overrides);
}
Also used : GeneratedValue(javax.persistence.GeneratedValue) Temporal(javax.persistence.Temporal) Column(javax.persistence.Column) Id(javax.persistence.Id) ElementCollection(javax.persistence.ElementCollection)

Aggregations

GeneratedValue (javax.persistence.GeneratedValue)10 Id (javax.persistence.Id)6 Column (javax.persistence.Column)4 ElementCollection (javax.persistence.ElementCollection)3 Temporal (javax.persistence.Temporal)3 XClass (org.hibernate.annotations.common.reflection.XClass)3 EmbeddedId (javax.persistence.EmbeddedId)2 Enumerated (javax.persistence.Enumerated)2 JoinColumn (javax.persistence.JoinColumn)2 ManyToOne (javax.persistence.ManyToOne)2 UniqueConstraint (javax.persistence.UniqueConstraint)2 AnnotationException (org.hibernate.AnnotationException)2 XProperty (org.hibernate.annotations.common.reflection.XProperty)2 IEntity (de.invesdwin.context.persistence.jpa.api.dao.entity.IEntity)1 Column (io.requery.Column)1 ForeignKey (io.requery.ForeignKey)1 Index (io.requery.Index)1 Lazy (io.requery.Lazy)1 Nullable (io.requery.Nullable)1 EnumOrdinalConverter (io.requery.converter.EnumOrdinalConverter)1