Search in sources :

Example 1 with Embedded

use of javax.persistence.Embedded in project querydsl by querydsl.

the class RooAnnotationProcessor method createConfiguration.

@Override
protected Configuration createConfiguration(RoundEnvironment roundEnv) {
    Class<? extends Annotation> entity = RooJpaEntity.class;
    Class<? extends Annotation> superType = MappedSuperclass.class;
    Class<? extends Annotation> embeddable = Embeddable.class;
    Class<? extends Annotation> embedded = Embedded.class;
    Class<? extends Annotation> skip = Transient.class;
    DefaultConfiguration conf = new JPAConfiguration(roundEnv, processingEnv, processingEnv.getOptions(), entity, superType, embeddable, embedded, skip);
    conf.setAlternativeEntityAnnotation(RooJpaActiveRecord.class);
    return conf;
}
Also used : JPAConfiguration(com.querydsl.apt.jpa.JPAConfiguration) MappedSuperclass(javax.persistence.MappedSuperclass) DefaultConfiguration(com.querydsl.apt.DefaultConfiguration) Embedded(javax.persistence.Embedded) Transient(javax.persistence.Transient) RooJpaEntity(org.springframework.roo.addon.jpa.entity.RooJpaEntity) Embeddable(javax.persistence.Embeddable)

Example 2 with Embedded

use of javax.persistence.Embedded in project cloudstack by apache.

the class SqlGenerator method buildAttributes.

protected void buildAttributes(Class<?> clazz, String tableName, AttributeOverride[] overrides, boolean embedded, boolean isId) {
    if (!embedded && clazz.getAnnotation(Entity.class) == null) {
        // except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself
        if (clazz.getAnnotation(MappedSuperclass.class) != null) {
            Field[] declaredFields = clazz.getDeclaredFields();
            _mappedSuperclassFields = (Field[]) ArrayUtils.addAll(_mappedSuperclassFields, declaredFields);
        }
        return;
    }
    Class<?> parent = clazz.getSuperclass();
    if (parent != null) {
        buildAttributes(parent, DbUtil.getTableName(parent), DbUtil.getAttributeOverrides(parent), false, false);
    }
    if (!embedded) {
        _tables.add(clazz);
        _ids.put(tableName, new ArrayList<Attribute>());
    }
    Field[] fields = clazz.getDeclaredFields();
    fields = (Field[]) ArrayUtils.addAll(fields, _mappedSuperclassFields);
    _mappedSuperclassFields = null;
    for (Field field : fields) {
        field.setAccessible(true);
        TableGenerator tg = field.getAnnotation(TableGenerator.class);
        if (tg != null) {
            _generators.put(field.getName(), tg);
        }
        if (!DbUtil.isPersistable(field)) {
            continue;
        }
        if (field.getAnnotation(Embedded.class) != null) {
            _embeddeds.add(field);
            Class<?> embeddedClass = field.getType();
            assert (embeddedClass.getAnnotation(Embeddable.class) != null) : "Class is not annotated with Embeddable: " + embeddedClass.getName();
            buildAttributes(embeddedClass, tableName, DbUtil.getAttributeOverrides(field), true, false);
            continue;
        }
        if (field.getAnnotation(EmbeddedId.class) != null) {
            _embeddeds.add(field);
            Class<?> embeddedClass = field.getType();
            assert (embeddedClass.getAnnotation(Embeddable.class) != null) : "Class is not annotated with Embeddable: " + embeddedClass.getName();
            buildAttributes(embeddedClass, tableName, DbUtil.getAttributeOverrides(field), true, true);
            continue;
        }
        Attribute attr = new Attribute(clazz, overrides, field, tableName, embedded, isId);
        if (attr.getColumnName().equals(GenericDao.REMOVED_COLUMN)) {
            attr.setTrue(Attribute.Flag.DaoGenerated);
            attr.setFalse(Attribute.Flag.Insertable);
            attr.setFalse(Attribute.Flag.Updatable);
            attr.setTrue(Attribute.Flag.TimeStamp);
            attr.setFalse(Attribute.Flag.Time);
            attr.setFalse(Attribute.Flag.Date);
            attr.setTrue(Attribute.Flag.Nullable);
            attr.setTrue(Attribute.Flag.Removed);
        }
        if (attr.isId()) {
            List<Attribute> attrs = _ids.get(tableName);
            attrs.add(attr);
        }
        _attributes.add(attr);
    }
}
Also used : Field(java.lang.reflect.Field) MappedSuperclass(javax.persistence.MappedSuperclass) EmbeddedId(javax.persistence.EmbeddedId) TableGenerator(javax.persistence.TableGenerator) Embedded(javax.persistence.Embedded)

Example 3 with Embedded

use of javax.persistence.Embedded in project midpoint by Evolveum.

the class ClassDefinitionParser method parseMethod.

private JpaLinkDefinition parseMethod(Method method) {
    // non-null if return type is Set<X>, null if it's X
    CollectionSpecification collectionSpecification;
    // X in return type, which is either X or Set<X>
    Type returnedContentType;
    if (Set.class.isAssignableFrom(method.getReturnType())) {
        // e.g. Set<RObject> or Set<String> or Set<REmbeddedReference<RFocus>>
        Type returnType = method.getGenericReturnType();
        if (!(returnType instanceof ParameterizedType)) {
            throw new IllegalStateException("Method " + method + " returns a non-parameterized collection");
        }
        returnedContentType = ((ParameterizedType) returnType).getActualTypeArguments()[0];
        collectionSpecification = new CollectionSpecification();
    } else {
        returnedContentType = method.getReturnType();
        collectionSpecification = null;
    }
    ItemPath itemPath = getJaxbName(method);
    String jpaName = getJpaName(method);
    Class jpaClass = getClass(returnedContentType);
    // sanity check
    if (Set.class.isAssignableFrom(jpaClass)) {
        throw new IllegalStateException("Collection within collection is not supported: method=" + method);
    }
    JpaLinkDefinition<? extends JpaDataNodeDefinition> linkDefinition;
    Any any = method.getAnnotation(Any.class);
    if (any != null) {
        JpaAnyContainerDefinition targetDefinition = new JpaAnyContainerDefinition(jpaClass);
        QName jaxbNameForAny = new QName(any.jaxbNameNamespace(), any.jaxbNameLocalPart());
        linkDefinition = new JpaLinkDefinition<>(jaxbNameForAny, jpaName, collectionSpecification, false, targetDefinition);
    } else if (ObjectReference.class.isAssignableFrom(jpaClass)) {
        boolean embedded = method.isAnnotationPresent(Embedded.class);
        // computing referenced entity type from returned content type like RObjectReference<RFocus> or REmbeddedReference<RRole>
        Class referencedJpaClass;
        if (returnedContentType instanceof ParameterizedType) {
            referencedJpaClass = getClass(((ParameterizedType) returnedContentType).getActualTypeArguments()[0]);
        } else {
            referencedJpaClass = RObject.class;
        }
        JpaReferenceDefinition targetDefinition = new JpaReferenceDefinition(jpaClass, referencedJpaClass);
        linkDefinition = new JpaLinkDefinition<>(itemPath, jpaName, collectionSpecification, embedded, targetDefinition);
    } else if (isEntity(jpaClass)) {
        JpaEntityDefinition content = parseClass(jpaClass);
        boolean embedded = method.isAnnotationPresent(Embedded.class) || jpaClass.isAnnotationPresent(Embeddable.class);
        linkDefinition = new JpaLinkDefinition<JpaDataNodeDefinition>(itemPath, jpaName, collectionSpecification, embedded, content);
    } else {
        boolean lob = method.isAnnotationPresent(Lob.class);
        boolean enumerated = method.isAnnotationPresent(Enumerated.class);
        //todo implement also lookup for @Table indexes
        boolean indexed = method.isAnnotationPresent(Index.class);
        boolean count = method.isAnnotationPresent(Count.class);
        Class jaxbClass = getJaxbClass(method, jpaClass);
        if (method.isAnnotationPresent(IdQueryProperty.class)) {
            if (collectionSpecification != null) {
                throw new IllegalStateException("ID property is not allowed to be multivalued; for method " + method);
            }
            itemPath = new ItemPath(new IdentifierPathSegment());
        } else if (method.isAnnotationPresent(OwnerIdGetter.class)) {
            if (collectionSpecification != null) {
                throw new IllegalStateException("Owner ID property is not allowed to be multivalued; for method " + method);
            }
            itemPath = new ItemPath(new ParentPathSegment(), new IdentifierPathSegment());
        }
        JpaPropertyDefinition propertyDefinition = new JpaPropertyDefinition(jpaClass, jaxbClass, lob, enumerated, indexed, count);
        // Note that properties are considered to be embedded
        linkDefinition = new JpaLinkDefinition<JpaDataNodeDefinition>(itemPath, jpaName, collectionSpecification, true, propertyDefinition);
    }
    return linkDefinition;
}
Also used : RPolyString(com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString) ParameterizedType(java.lang.reflect.ParameterizedType) ObjectReference(com.evolveum.midpoint.repo.sql.data.common.ObjectReference) RObject(com.evolveum.midpoint.repo.sql.data.common.RObject) Embedded(javax.persistence.Embedded) IdentifierPathSegment(com.evolveum.midpoint.prism.path.IdentifierPathSegment) ParentPathSegment(com.evolveum.midpoint.prism.path.ParentPathSegment) QName(javax.xml.namespace.QName) Embeddable(javax.persistence.Embeddable) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 4 with Embedded

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

the class SqlGenerator method buildAttributes.

protected void buildAttributes(Class<?> clazz, String tableName, AttributeOverride[] overrides, boolean embedded, boolean isId) {
    if (!embedded && clazz.getAnnotation(Entity.class) == null) {
        return;
    }
    Class<?> parent = clazz.getSuperclass();
    if (parent != null) {
        buildAttributes(parent, DbUtil.getTableName(parent), DbUtil.getAttributeOverrides(parent), false, false);
    }
    if (!embedded) {
        _tables.add(clazz);
        _ids.put(tableName, new ArrayList<Attribute>());
    }
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        TableGenerator tg = field.getAnnotation(TableGenerator.class);
        if (tg != null) {
            _generators.put(field.getName(), tg);
        }
        if (!DbUtil.isPersistable(field)) {
            continue;
        }
        if (field.getAnnotation(Embedded.class) != null) {
            _embeddeds.add(field);
            Class<?> embeddedClass = field.getType();
            assert (embeddedClass.getAnnotation(Embeddable.class) != null) : "Class is not annotated with Embeddable: " + embeddedClass.getName();
            buildAttributes(embeddedClass, tableName, DbUtil.getAttributeOverrides(field), true, false);
            continue;
        }
        if (field.getAnnotation(EmbeddedId.class) != null) {
            _embeddeds.add(field);
            Class<?> embeddedClass = field.getType();
            assert (embeddedClass.getAnnotation(Embeddable.class) != null) : "Class is not annotated with Embeddable: " + embeddedClass.getName();
            buildAttributes(embeddedClass, tableName, DbUtil.getAttributeOverrides(field), true, true);
            continue;
        }
        Attribute attr = new Attribute(clazz, overrides, field, tableName, embedded, isId);
        if (attr.getColumnName().equals(GenericDao.REMOVED_COLUMN)) {
            attr.setColumnName(GenericDao.REMOVED);
            attr.setTrue(Attribute.Flag.DaoGenerated);
            attr.setFalse(Attribute.Flag.Insertable);
            attr.setFalse(Attribute.Flag.Updatable);
            attr.setTrue(Attribute.Flag.TimeStamp);
            attr.setFalse(Attribute.Flag.Time);
            attr.setFalse(Attribute.Flag.Date);
            attr.setTrue(Attribute.Flag.Nullable);
            attr.setTrue(Attribute.Flag.Removed);
        }
        if (attr.isId()) {
            List<Attribute> attrs = _ids.get(tableName);
            attrs.add(attr);
        }
        _attributes.add(attr);
    }
}
Also used : Field(java.lang.reflect.Field) EmbeddedId(javax.persistence.EmbeddedId) TableGenerator(javax.persistence.TableGenerator) Embedded(javax.persistence.Embedded)

Aggregations

Embedded (javax.persistence.Embedded)4 Field (java.lang.reflect.Field)2 Embeddable (javax.persistence.Embeddable)2 EmbeddedId (javax.persistence.EmbeddedId)2 MappedSuperclass (javax.persistence.MappedSuperclass)2 TableGenerator (javax.persistence.TableGenerator)2 IdentifierPathSegment (com.evolveum.midpoint.prism.path.IdentifierPathSegment)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 ParentPathSegment (com.evolveum.midpoint.prism.path.ParentPathSegment)1 ObjectReference (com.evolveum.midpoint.repo.sql.data.common.ObjectReference)1 RObject (com.evolveum.midpoint.repo.sql.data.common.RObject)1 RPolyString (com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString)1 DefaultConfiguration (com.querydsl.apt.DefaultConfiguration)1 JPAConfiguration (com.querydsl.apt.jpa.JPAConfiguration)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Transient (javax.persistence.Transient)1 QName (javax.xml.namespace.QName)1 RooJpaEntity (org.springframework.roo.addon.jpa.entity.RooJpaEntity)1