Search in sources :

Example 6 with Id

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

the class JPAOverriddenAnnotationReaderTest method testIdRelatedAnnotations.

@Test
public void testIdRelatedAnnotations() throws Exception {
    XMLContext context = buildContext("org/hibernate/test/annotations/reflection/orm.xml");
    Method method = Administration.class.getDeclaredMethod("getId");
    JPAOverriddenAnnotationReader reader = new JPAOverriddenAnnotationReader(method, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNull(reader.getAnnotation(Id.class));
    assertNull(reader.getAnnotation(Column.class));
    Field field = Administration.class.getDeclaredField("id");
    reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(Id.class));
    assertNotNull(reader.getAnnotation(GeneratedValue.class));
    assertEquals(GenerationType.SEQUENCE, reader.getAnnotation(GeneratedValue.class).strategy());
    assertEquals("generator", reader.getAnnotation(GeneratedValue.class).generator());
    assertNotNull(reader.getAnnotation(SequenceGenerator.class));
    assertEquals("seq", reader.getAnnotation(SequenceGenerator.class).sequenceName());
    assertNotNull(reader.getAnnotation(Columns.class));
    assertEquals(1, reader.getAnnotation(Columns.class).columns().length);
    assertEquals("fld_id", reader.getAnnotation(Columns.class).columns()[0].name());
    assertNotNull(reader.getAnnotation(Temporal.class));
    assertEquals(TemporalType.DATE, reader.getAnnotation(Temporal.class).value());
    context = buildContext("org/hibernate/test/annotations/reflection/metadata-complete.xml");
    method = Administration.class.getDeclaredMethod("getId");
    reader = new JPAOverriddenAnnotationReader(method, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull("Default access type when not defined in metadata complete should be property", reader.getAnnotation(Id.class));
    field = Administration.class.getDeclaredField("id");
    reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNull("Default access type when not defined in metadata complete should be property", reader.getAnnotation(Id.class));
    method = BusTrip.class.getDeclaredMethod("getId");
    reader = new JPAOverriddenAnnotationReader(method, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNull(reader.getAnnotation(EmbeddedId.class));
    field = BusTrip.class.getDeclaredField("id");
    reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(EmbeddedId.class));
    assertNotNull(reader.getAnnotation(AttributeOverrides.class));
    assertEquals(1, reader.getAnnotation(AttributeOverrides.class).value().length);
}
Also used : XMLContext(org.hibernate.cfg.annotations.reflection.XMLContext) SequenceGenerator(javax.persistence.SequenceGenerator) JPAOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) Columns(org.hibernate.annotations.Columns) JoinColumns(javax.persistence.JoinColumns) EmbeddedId(javax.persistence.EmbeddedId) Method(java.lang.reflect.Method) GeneratedValue(javax.persistence.GeneratedValue) Field(java.lang.reflect.Field) AttributeOverrides(javax.persistence.AttributeOverrides) Temporal(javax.persistence.Temporal) Column(javax.persistence.Column) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) Id(javax.persistence.Id) EmbeddedId(javax.persistence.EmbeddedId) Test(org.junit.Test)

Example 7 with Id

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

the class AnnotationBinder method addProperty.

private static int addProperty(PropertyContainer propertyContainer, XProperty property, List<PropertyData> inFlightPropertyDataList, MetadataBuildingContext context) {
    // and if so, skip it..
    for (PropertyData propertyData : inFlightPropertyDataList) {
        if (propertyData.getPropertyName().equals(property.getName())) {
            // EARLY EXIT!!!
            return 0;
        }
    }
    final XClass declaringClass = propertyContainer.getDeclaringClass();
    final XClass entity = propertyContainer.getEntityAtStake();
    int idPropertyCounter = 0;
    PropertyData propertyAnnotatedElement = new PropertyInferredData(declaringClass, property, propertyContainer.getClassLevelAccessType().getType(), context.getBuildingOptions().getReflectionManager());
    /*
		 * put element annotated by @Id in front
		 * since it has to be parsed beforeQuery any association by Hibernate
		 */
    final XAnnotatedElement element = propertyAnnotatedElement.getProperty();
    if (element.isAnnotationPresent(Id.class) || element.isAnnotationPresent(EmbeddedId.class)) {
        inFlightPropertyDataList.add(0, propertyAnnotatedElement);
        /**
			 * The property must be put in hibernate.properties as it's a system wide property. Fixable?
			 * TODO support true/false/default on the property instead of present / not present
			 * TODO is @Column mandatory?
			 * TODO add method support
			 */
        if (context.getBuildingOptions().isSpecjProprietarySyntaxEnabled()) {
            if (element.isAnnotationPresent(Id.class) && element.isAnnotationPresent(Column.class)) {
                String columnName = element.getAnnotation(Column.class).name();
                for (XProperty prop : declaringClass.getDeclaredProperties(AccessType.FIELD.getType())) {
                    if (!prop.isAnnotationPresent(MapsId.class)) {
                        /**
							 * The detection of a configured individual JoinColumn differs between Annotation
							 * and XML configuration processing.
							 */
                        boolean isRequiredAnnotationPresent = false;
                        JoinColumns groupAnnotation = prop.getAnnotation(JoinColumns.class);
                        if ((prop.isAnnotationPresent(JoinColumn.class) && prop.getAnnotation(JoinColumn.class).name().equals(columnName))) {
                            isRequiredAnnotationPresent = true;
                        } else if (prop.isAnnotationPresent(JoinColumns.class)) {
                            for (JoinColumn columnAnnotation : groupAnnotation.value()) {
                                if (columnName.equals(columnAnnotation.name())) {
                                    isRequiredAnnotationPresent = true;
                                    break;
                                }
                            }
                        }
                        if (isRequiredAnnotationPresent) {
                            //create a PropertyData fpr the specJ property holding the mapping
                            PropertyData specJPropertyData = new PropertyInferredData(declaringClass, //same dec
                            prop, // the actual @XToOne property
                            propertyContainer.getClassLevelAccessType().getType(), //TODO we should get the right accessor but the same as id would do
                            context.getBuildingOptions().getReflectionManager());
                            context.getMetadataCollector().addPropertyAnnotatedWithMapsIdSpecj(entity, specJPropertyData, element.toString());
                        }
                    }
                }
            }
        }
        if (element.isAnnotationPresent(ManyToOne.class) || element.isAnnotationPresent(OneToOne.class)) {
            context.getMetadataCollector().addToOneAndIdProperty(entity, propertyAnnotatedElement);
        }
        idPropertyCounter++;
    } else {
        inFlightPropertyDataList.add(propertyAnnotatedElement);
    }
    if (element.isAnnotationPresent(MapsId.class)) {
        context.getMetadataCollector().addPropertyAnnotatedWithMapsId(entity, propertyAnnotatedElement);
    }
    return idPropertyCounter;
}
Also used : MapsId(javax.persistence.MapsId) XProperty(org.hibernate.annotations.common.reflection.XProperty) EmbeddedId(javax.persistence.EmbeddedId) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) XAnnotatedElement(org.hibernate.annotations.common.reflection.XAnnotatedElement) XClass(org.hibernate.annotations.common.reflection.XClass) UniqueConstraint(javax.persistence.UniqueConstraint) Constraint(org.hibernate.mapping.Constraint) ManyToOne(javax.persistence.ManyToOne) OneToOne(javax.persistence.OneToOne) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) MapKeyColumn(javax.persistence.MapKeyColumn) OrderColumn(javax.persistence.OrderColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(javax.persistence.Column) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) JoinColumn(javax.persistence.JoinColumn) MapsId(javax.persistence.MapsId) NaturalId(org.hibernate.annotations.NaturalId) EmbeddedId(javax.persistence.EmbeddedId) Id(javax.persistence.Id) CollectionId(org.hibernate.annotations.CollectionId)

Example 8 with Id

use of javax.persistence.Id in project eweb4j-framework by laiweiwei.

the class PojoAnnotationConfig method getProperties.

private static List<Property> getProperties(Class<?> clazz, final List<Property> pList, final boolean requireSuper, Log log) throws Throwable {
    List<Property> result = new ArrayList<Property>();
    ReflectUtil ru;
    try {
        ru = new ReflectUtil(clazz);
        ru.setRequiredSuper(requireSuper);
    } catch (Throwable e) {
        log.warn(e.toString(), e);
        throw e;
    }
    for (Field f : ru.getFields()) {
        if (Collection.class.isAssignableFrom(f.getType()))
            continue;
        String name = f.getName();
        Method getter = ru.getGetter(name);
        if (getter == null)
            continue;
        Ignore igAnn = f.getAnnotation(Ignore.class);
        if (igAnn == null)
            igAnn = getter.getAnnotation(Ignore.class);
        if (igAnn != null)
            continue;
        Transient trans = f.getAnnotation(Transient.class);
        if (trans == null)
            trans = getter.getAnnotation(Transient.class);
        if (trans != null)
            continue;
        OneToMany manyAnn = getter.getAnnotation(OneToMany.class);
        if (manyAnn != null)
            continue;
        else {
            manyAnn = f.getAnnotation(OneToMany.class);
            if (manyAnn != null)
                continue;
        }
        ManyToMany manyManyAnn = getter.getAnnotation(ManyToMany.class);
        if (manyManyAnn != null)
            continue;
        else {
            manyManyAnn = f.getAnnotation(ManyToMany.class);
            if (manyManyAnn != null)
                continue;
        }
        Property p = new Property();
        if (Long.class.isAssignableFrom(f.getType()) || long.class.isAssignableFrom(f.getType()))
            p.setSize("20");
        else if (Integer.class.isAssignableFrom(f.getType()) || int.class.isAssignableFrom(f.getType()))
            p.setSize("8");
        else if (String.class.isAssignableFrom(f.getType()))
            p.setSize("255");
        else if (Boolean.class.isAssignableFrom(f.getType()) || boolean.class.isAssignableFrom(f.getType()))
            p.setSize("");
        else if (Float.class.isAssignableFrom(f.getType()) || float.class.isAssignableFrom(f.getType()))
            p.setSize("8");
        Id idAnn = getter.getAnnotation(Id.class);
        if (idAnn == null)
            idAnn = f.getAnnotation(Id.class);
        if (idAnn != null) {
            if (pList != null && hasIdProperty(pList))
                continue;
            p.setAutoIncrement("1");
            p.setPk("1");
            p.setSize("20");
        }
        Column colAnn = getter.getAnnotation(Column.class);
        if (colAnn == null) {
            colAnn = f.getAnnotation(Column.class);
        }
        String column = colAnn == null ? "" : colAnn.name();
        column = "".equals(column.trim()) ? name : column;
        p.setName(name);
        p.setColumn(column);
        p.setType(f.getType().getName());
        p.setNotNull("false");
        if (colAnn != null) {
            // int size = colAnn.length();
            p.setNotNull(String.valueOf(!colAnn.nullable()));
            p.setUnique(String.valueOf(colAnn.unique()));
        }
        if (ClassUtil.isPojo(f.getType())) {
            OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
            if (oneAnn == null)
                oneAnn = f.getAnnotation(OneToOne.class);
            ManyToOne manyToOneAnn = null;
            if (oneAnn == null) {
                manyToOneAnn = getter.getAnnotation(ManyToOne.class);
                if (manyToOneAnn == null)
                    manyToOneAnn = f.getAnnotation(ManyToOne.class);
            }
            if (oneAnn != null || manyToOneAnn != null) {
                if (oneAnn != null)
                    p.setType(PropType.ONE_ONE);
                else
                    p.setType(PropType.MANY_ONE);
                JoinColumn joinColumn = getter.getAnnotation(JoinColumn.class);
                if (joinColumn == null)
                    joinColumn = f.getAnnotation(JoinColumn.class);
                if (joinColumn != null && joinColumn.name().trim().length() > 0)
                    p.setColumn(joinColumn.name());
                else
                    p.setColumn(f.getName() + "_id");
                p.setRelProperty(null);
                String refCol = null;
                if (joinColumn != null && joinColumn.referencedColumnName().trim().length() > 0) {
                    refCol = joinColumn.referencedColumnName();
                    if (refCol != null && refCol.trim().length() > 0) {
                        String relField = ORMConfigBeanUtil.getField(f.getType(), refCol);
                        if (relField != null && relField.trim().length() > 0)
                            p.setRelProperty(relField);
                    }
                }
                p.setRelClass(f.getType());
                p.setSize("20");
            }
        }
        result.add(p);
    }
    return result;
}
Also used : Ignore(org.eweb4j.orm.annotation.Ignore) ArrayList(java.util.ArrayList) ManyToMany(javax.persistence.ManyToMany) Method(java.lang.reflect.Method) OneToMany(javax.persistence.OneToMany) ManyToOne(javax.persistence.ManyToOne) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) OneToOne(javax.persistence.OneToOne) JoinColumn(javax.persistence.JoinColumn) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) Transient(javax.persistence.Transient) Id(javax.persistence.Id) Property(org.eweb4j.orm.config.bean.Property)

Example 9 with Id

use of javax.persistence.Id 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)

Example 10 with Id

use of javax.persistence.Id in project Activiti by Activiti.

the class JPAEntityScanner method getIdField.

private Field getIdField(Class<?> clazz) {
    Field idField = null;
    Field[] fields = clazz.getDeclaredFields();
    Id idAnnotation = null;
    for (Field field : fields) {
        idAnnotation = field.getAnnotation(Id.class);
        if (idAnnotation != null) {
            idField = field;
            break;
        }
    }
    if (idField == null) {
        // Check superClass for fields with @Id, since getDeclaredFields does
        // not return superclass-fields.
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null && !superClass.equals(Object.class)) {
            // Recursively go up class hierarchy
            idField = getIdField(superClass);
        }
    }
    return idField;
}
Also used : Field(java.lang.reflect.Field) Id(javax.persistence.Id)

Aggregations

Id (javax.persistence.Id)12 Column (javax.persistence.Column)8 Method (java.lang.reflect.Method)5 Field (java.lang.reflect.Field)4 EmbeddedId (javax.persistence.EmbeddedId)4 GeneratedValue (javax.persistence.GeneratedValue)4 JoinColumn (javax.persistence.JoinColumn)4 ManyToOne (javax.persistence.ManyToOne)4 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)3 ElementCollection (javax.persistence.ElementCollection)3 JoinColumns (javax.persistence.JoinColumns)3 OneToOne (javax.persistence.OneToOne)3 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)3 PrimaryKeyJoinColumns (javax.persistence.PrimaryKeyJoinColumns)3 Temporal (javax.persistence.Temporal)3 ArrayList (java.util.ArrayList)2 ManyToMany (javax.persistence.ManyToMany)2 MapKeyColumn (javax.persistence.MapKeyColumn)2 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)2 MapKeyJoinColumns (javax.persistence.MapKeyJoinColumns)2