Search in sources :

Example 6 with XProperty

use of org.hibernate.annotations.common.reflection.XProperty in project hibernate-orm by hibernate.

the class MapBinder method bindKeyFromAssociationTable.

private void bindKeyFromAssociationTable(XClass collType, Map persistentClasses, String mapKeyPropertyName, XProperty property, boolean isEmbedded, MetadataBuildingContext buildingContext, Ejb3Column[] mapKeyColumns, Ejb3JoinColumn[] mapKeyManyToManyColumns, String targetPropertyName) {
    if (mapKeyPropertyName != null) {
        //this is an EJB3 @MapKey
        PersistentClass associatedClass = (PersistentClass) persistentClasses.get(collType.getName());
        if (associatedClass == null)
            throw new AnnotationException("Associated class not found: " + collType);
        Property mapProperty = BinderHelper.findPropertyByName(associatedClass, mapKeyPropertyName);
        if (mapProperty == null) {
            throw new AnnotationException("Map key property not found: " + collType + "." + mapKeyPropertyName);
        }
        org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection;
        // HHH-11005 - if InheritanceType.JOINED then need to find class defining the column
        InheritanceState inheritanceState = inheritanceStatePerClass.get(collType);
        PersistentClass targetPropertyPersistentClass = InheritanceType.JOINED.equals(inheritanceState.getType()) ? mapProperty.getPersistentClass() : associatedClass;
        Value indexValue = createFormulatedValue(mapProperty.getValue(), map, targetPropertyName, associatedClass, targetPropertyPersistentClass, buildingContext);
        map.setIndex(indexValue);
    } else {
        //this is a true Map mapping
        //TODO ugly copy/pastle from CollectionBinder.bindManyToManySecondPass
        String mapKeyType;
        Class target = void.class;
        /*
			 * target has priority over reflection for the map key type
			 * JPA 2 has priority
			 */
        if (property.isAnnotationPresent(MapKeyClass.class)) {
            target = property.getAnnotation(MapKeyClass.class).value();
        }
        if (!void.class.equals(target)) {
            mapKeyType = target.getName();
        } else {
            mapKeyType = property.getMapKey().getName();
        }
        PersistentClass collectionEntity = (PersistentClass) persistentClasses.get(mapKeyType);
        boolean isIndexOfEntities = collectionEntity != null;
        ManyToOne element = null;
        org.hibernate.mapping.Map mapValue = (org.hibernate.mapping.Map) this.collection;
        if (isIndexOfEntities) {
            element = new ManyToOne(buildingContext.getMetadataCollector(), mapValue.getCollectionTable());
            mapValue.setIndex(element);
            element.setReferencedEntityName(mapKeyType);
            //element.setFetchMode( fetchMode );
            //element.setLazy( fetchMode != FetchMode.JOIN );
            //make the second join non lazy
            element.setFetchMode(FetchMode.JOIN);
            element.setLazy(false);
        //does not make sense for a map key element.setIgnoreNotFound( ignoreNotFound );
        } else {
            XClass keyXClass;
            AnnotatedClassType classType;
            if (BinderHelper.PRIMITIVE_NAMES.contains(mapKeyType)) {
                classType = AnnotatedClassType.NONE;
                keyXClass = null;
            } else {
                try {
                    keyXClass = buildingContext.getBuildingOptions().getReflectionManager().classForName(mapKeyType);
                } catch (ClassLoadingException e) {
                    throw new AnnotationException("Unable to find class: " + mapKeyType, e);
                }
                classType = buildingContext.getMetadataCollector().getClassType(keyXClass);
                // force in case of attribute override naming the key
                if (isEmbedded || mappingDefinedAttributeOverrideOnMapKey(property)) {
                    classType = AnnotatedClassType.EMBEDDABLE;
                }
            }
            CollectionPropertyHolder holder = PropertyHolderBuilder.buildPropertyHolder(mapValue, StringHelper.qualify(mapValue.getRole(), "mapkey"), keyXClass, property, propertyHolder, buildingContext);
            // 'propertyHolder' is the PropertyHolder for the owner of the collection
            // 'holder' is the CollectionPropertyHolder.
            // 'property' is the collection XProperty
            propertyHolder.startingProperty(property);
            holder.prepare(property);
            PersistentClass owner = mapValue.getOwner();
            AccessType accessType;
            // String accessType = access != null ? access.value() : null;
            if (owner.getIdentifierProperty() != null) {
                accessType = owner.getIdentifierProperty().getPropertyAccessorName().equals("property") ? AccessType.PROPERTY : AccessType.FIELD;
            } else if (owner.getIdentifierMapper() != null && owner.getIdentifierMapper().getPropertySpan() > 0) {
                Property prop = (Property) owner.getIdentifierMapper().getPropertyIterator().next();
                accessType = prop.getPropertyAccessorName().equals("property") ? AccessType.PROPERTY : AccessType.FIELD;
            } else {
                throw new AssertionFailure("Unable to guess collection property accessor name");
            }
            if (AnnotatedClassType.EMBEDDABLE.equals(classType)) {
                EntityBinder entityBinder = new EntityBinder();
                PropertyData inferredData;
                if (isHibernateExtensionMapping()) {
                    inferredData = new PropertyPreloadedData(AccessType.PROPERTY, "index", keyXClass);
                } else {
                    //"key" is the JPA 2 prefix for map keys
                    inferredData = new PropertyPreloadedData(AccessType.PROPERTY, "key", keyXClass);
                }
                //TODO be smart with isNullable
                Component component = AnnotationBinder.fillComponent(holder, inferredData, accessType, true, entityBinder, false, false, true, buildingContext, inheritanceStatePerClass);
                mapValue.setIndex(component);
            } else {
                SimpleValueBinder elementBinder = new SimpleValueBinder();
                elementBinder.setBuildingContext(buildingContext);
                elementBinder.setReturnedClassName(mapKeyType);
                Ejb3Column[] elementColumns = mapKeyColumns;
                if (elementColumns == null || elementColumns.length == 0) {
                    elementColumns = new Ejb3Column[1];
                    Ejb3Column column = new Ejb3Column();
                    column.setImplicit(false);
                    column.setNullable(true);
                    column.setLength(Ejb3Column.DEFAULT_COLUMN_LENGTH);
                    column.setLogicalColumnName(Collection.DEFAULT_KEY_COLUMN_NAME);
                    //TODO create an EMPTY_JOINS collection
                    column.setJoins(new HashMap<String, Join>());
                    column.setBuildingContext(buildingContext);
                    column.bind();
                    elementColumns[0] = column;
                }
                //override the table
                for (Ejb3Column column : elementColumns) {
                    column.setTable(mapValue.getCollectionTable());
                }
                elementBinder.setColumns(elementColumns);
                //do not call setType as it extract the type from @Type
                //the algorithm generally does not apply for map key anyway
                elementBinder.setKey(true);
                elementBinder.setType(property, keyXClass, this.collection.getOwnerEntityName(), holder.mapKeyAttributeConverterDescriptor(property, keyXClass));
                elementBinder.setPersistentClassName(propertyHolder.getEntityName());
                elementBinder.setAccessType(accessType);
                mapValue.setIndex(elementBinder.make());
            }
        }
        //FIXME pass the Index Entity JoinColumns
        if (!collection.isOneToMany()) {
            //index column shoud not be null
            for (Ejb3JoinColumn col : mapKeyManyToManyColumns) {
                col.forceNotNull();
            }
        }
        if (element != null) {
            final javax.persistence.ForeignKey foreignKey = getMapKeyForeignKey(property);
            if (foreignKey != null) {
                if (foreignKey.value() == ConstraintMode.NO_CONSTRAINT) {
                    element.setForeignKeyName("none");
                } else {
                    element.setForeignKeyName(StringHelper.nullIfEmpty(foreignKey.name()));
                    element.setForeignKeyDefinition(StringHelper.nullIfEmpty(foreignKey.foreignKeyDefinition()));
                }
            }
        }
        if (isIndexOfEntities) {
            bindManytoManyInverseFk(collectionEntity, mapKeyManyToManyColumns, element, //a map key column has no unique constraint
            false, buildingContext);
        }
    }
}
Also used : PropertyData(org.hibernate.cfg.PropertyData) PropertyPreloadedData(org.hibernate.cfg.PropertyPreloadedData) XClass(org.hibernate.annotations.common.reflection.XClass) ManyToOne(org.hibernate.mapping.ManyToOne) AnnotationException(org.hibernate.AnnotationException) Ejb3Column(org.hibernate.cfg.Ejb3Column) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty) AccessType(org.hibernate.cfg.AccessType) PersistentClass(org.hibernate.mapping.PersistentClass) CollectionPropertyHolder(org.hibernate.cfg.CollectionPropertyHolder) AssertionFailure(org.hibernate.AssertionFailure) ClassLoadingException(org.hibernate.annotations.common.reflection.ClassLoadingException) Join(org.hibernate.mapping.Join) InheritanceState(org.hibernate.cfg.InheritanceState) SimpleValue(org.hibernate.mapping.SimpleValue) DependantValue(org.hibernate.mapping.DependantValue) Value(org.hibernate.mapping.Value) MapKeyClass(javax.persistence.MapKeyClass) PersistentClass(org.hibernate.mapping.PersistentClass) XClass(org.hibernate.annotations.common.reflection.XClass) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) Map(java.util.Map) HashMap(java.util.HashMap) AnnotatedClassType(org.hibernate.cfg.AnnotatedClassType)

Example 7 with XProperty

use of org.hibernate.annotations.common.reflection.XProperty in project hibernate-orm by hibernate.

the class Ejb3Column method extractDataFromPropertyData.

//must only be called afterQuery all setters are defined and beforeQuery bind
private void extractDataFromPropertyData(PropertyData inferredData) {
    if (inferredData != null) {
        XProperty property = inferredData.getProperty();
        if (property != null) {
            processExpression(property.getAnnotation(ColumnTransformer.class));
            ColumnTransformers annotations = property.getAnnotation(ColumnTransformers.class);
            if (annotations != null) {
                for (ColumnTransformer annotation : annotations.value()) {
                    processExpression(annotation);
                }
            }
        }
    }
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) ColumnTransformers(org.hibernate.annotations.ColumnTransformers) ColumnTransformer(org.hibernate.annotations.ColumnTransformer)

Example 8 with XProperty

use of org.hibernate.annotations.common.reflection.XProperty in project hibernate-orm by hibernate.

the class MyStringType method assertInternallyConsistent.

/**
	 * Verifies that the specified parameters are internally consistent and valid, and then returns the value that
	 * should be persisted to the database based on the input parameters ("[table-name].[field-name]" or
	 * "[table-name].[field-name].[optional-suffix]"), so that we can later check that the parameters were externally
	 * consistent.
	 */
protected String assertInternallyConsistent(Properties params) {
    Boolean dynamic = Boolean.valueOf(params.getProperty(DynamicParameterizedType.IS_DYNAMIC));
    Assert.assertTrue(dynamic);
    String returnedClass = params.getProperty(DynamicParameterizedType.RETURNED_CLASS);
    Assert.assertEquals(String.class.getName(), returnedClass);
    Boolean primaryKey = Boolean.valueOf(params.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY));
    Assert.assertFalse(primaryKey);
    String accessType = params.getProperty(DynamicParameterizedType.ACCESS_TYPE);
    Assert.assertEquals("field", accessType);
    String entity = params.getProperty(DynamicParameterizedType.ENTITY);
    String propertyName = params.getProperty(DynamicParameterizedType.PROPERTY);
    XProperty xproperty = (XProperty) params.get(DynamicParameterizedType.XPROPERTY);
    Assert.assertEquals(propertyName, xproperty.getName());
    Assert.assertEquals(entity, xproperty.getDeclaringClass().getName());
    Assert.assertEquals(String.class.getName(), xproperty.getType().getName());
    String tableName = propertyName.toUpperCase().split("_")[0];
    String columnName = propertyName.toUpperCase().split("_")[1];
    ParameterType parameterType = (ParameterType) params.get(DynamicParameterizedType.PARAMETER_TYPE);
    Assert.assertEquals(1, parameterType.getColumns().length);
    Assert.assertEquals(columnName, parameterType.getColumns()[0]);
    Assert.assertEquals(String.class, parameterType.getReturnedClass());
    Assert.assertEquals(tableName, parameterType.getTable());
    String value = tableName + "." + columnName;
    if (params.containsKey("suffix")) {
        value += "." + params.getProperty("suffix").toUpperCase();
    }
    return value;
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty)

Example 9 with XProperty

use of org.hibernate.annotations.common.reflection.XProperty in project hibernate-orm by hibernate.

the class AuditedPropertiesReader method readAuditOverrides.

/**
	 * Recursively constructs sets of audited and not audited properties and classes which behavior has been overridden
	 * using {@link AuditOverride} annotation.
	 *
	 * @param clazz Class that is being processed. Currently mapped entity shall be passed during first invocation.
	 */
private void readAuditOverrides(XClass clazz) {
    /* TODO: Code to remove with @Audited.auditParents - start. */
    final Audited allClassAudited = clazz.getAnnotation(Audited.class);
    if (allClassAudited != null && allClassAudited.auditParents().length > 0) {
        for (Class c : allClassAudited.auditParents()) {
            final XClass parentClass = reflectionManager.toXClass(c);
            checkSuperclass(clazz, parentClass);
            if (!overriddenNotAuditedClasses.contains(parentClass)) {
                // If the class has not been marked as not audited by the subclass.
                overriddenAuditedClasses.add(parentClass);
            }
        }
    }
    /* TODO: Code to remove with @Audited.auditParents - finish. */
    final List<AuditOverride> auditOverrides = computeAuditOverrides(clazz);
    for (AuditOverride auditOverride : auditOverrides) {
        if (auditOverride.forClass() != void.class) {
            final XClass overrideClass = reflectionManager.toXClass(auditOverride.forClass());
            checkSuperclass(clazz, overrideClass);
            final String propertyName = auditOverride.name();
            if (!StringTools.isEmpty(propertyName)) {
                // Override @Audited annotation on property level.
                final XProperty property = getProperty(overrideClass, propertyName);
                if (auditOverride.isAudited()) {
                    if (!overriddenNotAuditedProperties.contains(property)) {
                        // If the property has not been marked as not audited by the subclass.
                        overriddenAuditedProperties.add(property);
                    }
                } else {
                    if (!overriddenAuditedProperties.contains(property)) {
                        // If the property has not been marked as audited by the subclass.
                        overriddenNotAuditedProperties.add(property);
                    }
                }
            } else {
                // Override @Audited annotation on class level.
                if (auditOverride.isAudited()) {
                    if (!overriddenNotAuditedClasses.contains(overrideClass)) {
                        // If the class has not been marked as not audited by the subclass.
                        overriddenAuditedClasses.add(overrideClass);
                    }
                } else {
                    if (!overriddenAuditedClasses.contains(overrideClass)) {
                        // If the class has not been marked as audited by the subclass.
                        overriddenNotAuditedClasses.add(overrideClass);
                    }
                }
            }
        }
    }
    final XClass superclass = clazz.getSuperclass();
    if (!clazz.isInterface() && !Object.class.getName().equals(superclass.getName())) {
        readAuditOverrides(superclass);
    }
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) Audited(org.hibernate.envers.Audited) NotAudited(org.hibernate.envers.NotAudited) AuditOverride(org.hibernate.envers.AuditOverride) XClass(org.hibernate.annotations.common.reflection.XClass) XClass(org.hibernate.annotations.common.reflection.XClass)

Example 10 with XProperty

use of org.hibernate.annotations.common.reflection.XProperty in project hibernate-orm by hibernate.

the class RevisionInfoConfiguration method searchForRevisionInfoCfgInProperties.

private void searchForRevisionInfoCfgInProperties(XClass clazz, ReflectionManager reflectionManager, MutableBoolean revisionNumberFound, MutableBoolean revisionTimestampFound, MutableBoolean modifiedEntityNamesFound, String accessType) {
    for (XProperty property : clazz.getDeclaredProperties(accessType)) {
        final RevisionNumber revisionNumber = property.getAnnotation(RevisionNumber.class);
        final RevisionTimestamp revisionTimestamp = property.getAnnotation(RevisionTimestamp.class);
        final ModifiedEntityNames modifiedEntityNames = property.getAnnotation(ModifiedEntityNames.class);
        if (revisionNumber != null) {
            if (revisionNumberFound.isSet()) {
                throw new MappingException("Only one property may be annotated with @RevisionNumber!");
            }
            final XClass revisionNumberClass = property.getType();
            if (reflectionManager.equals(revisionNumberClass, Integer.class) || reflectionManager.equals(revisionNumberClass, Integer.TYPE)) {
                revisionInfoIdData = new PropertyData(property.getName(), property.getName(), accessType, null);
                revisionNumberFound.set();
            } else if (reflectionManager.equals(revisionNumberClass, Long.class) || reflectionManager.equals(revisionNumberClass, Long.TYPE)) {
                revisionInfoIdData = new PropertyData(property.getName(), property.getName(), accessType, null);
                revisionNumberFound.set();
                // The default is integer
                revisionPropType = "long";
            } else {
                throw new MappingException("The field annotated with @RevisionNumber must be of type " + "int, Integer, long or Long");
            }
            // Getting the @Column definition of the revision number property, to later use that info to
            // generate the same mapping for the relation from an audit table's revision number to the
            // revision entity revision number.
            final Column revisionPropColumn = property.getAnnotation(Column.class);
            if (revisionPropColumn != null) {
                revisionPropSqlType = revisionPropColumn.columnDefinition();
            }
        }
        if (revisionTimestamp != null) {
            if (revisionTimestampFound.isSet()) {
                throw new MappingException("Only one property may be annotated with @RevisionTimestamp!");
            }
            final XClass revisionTimestampClass = property.getType();
            if (reflectionManager.equals(revisionTimestampClass, Long.class) || reflectionManager.equals(revisionTimestampClass, Long.TYPE) || reflectionManager.equals(revisionTimestampClass, Date.class) || reflectionManager.equals(revisionTimestampClass, java.sql.Date.class)) {
                revisionInfoTimestampData = new PropertyData(property.getName(), property.getName(), accessType, null);
                revisionTimestampFound.set();
            } else {
                throw new MappingException("The field annotated with @RevisionTimestamp must be of type " + "long, Long, java.util.Date or java.sql.Date");
            }
        }
        if (modifiedEntityNames != null) {
            if (modifiedEntityNamesFound.isSet()) {
                throw new MappingException("Only one property may be annotated with @ModifiedEntityNames!");
            }
            final XClass modifiedEntityNamesClass = property.getType();
            if (reflectionManager.equals(modifiedEntityNamesClass, Set.class) && reflectionManager.equals(property.getElementClass(), String.class)) {
                modifiedEntityNamesData = new PropertyData(property.getName(), property.getName(), accessType, null);
                modifiedEntityNamesFound.set();
            } else {
                throw new MappingException("The field annotated with @ModifiedEntityNames must be of Set<String> type.");
            }
        }
    }
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) ModifiedEntityNames(org.hibernate.envers.ModifiedEntityNames) PropertyData(org.hibernate.envers.internal.entities.PropertyData) Set(java.util.Set) RevisionTimestamp(org.hibernate.envers.RevisionTimestamp) RevisionNumber(org.hibernate.envers.RevisionNumber) XClass(org.hibernate.annotations.common.reflection.XClass) Date(java.util.Date) MappingException(org.hibernate.MappingException) Column(javax.persistence.Column)

Aggregations

XProperty (org.hibernate.annotations.common.reflection.XProperty)20 XClass (org.hibernate.annotations.common.reflection.XClass)9 AnnotationException (org.hibernate.AnnotationException)7 HashMap (java.util.HashMap)6 ManyToOne (javax.persistence.ManyToOne)6 EmbeddedId (javax.persistence.EmbeddedId)5 MappingException (org.hibernate.MappingException)5 Component (org.hibernate.mapping.Component)5 Column (javax.persistence.Column)4 Id (javax.persistence.Id)4 JoinColumn (javax.persistence.JoinColumn)4 Property (org.hibernate.mapping.Property)4 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)3 GeneratedValue (javax.persistence.GeneratedValue)3 JoinColumns (javax.persistence.JoinColumns)3 ManyToMany (javax.persistence.ManyToMany)3 MapsId (javax.persistence.MapsId)3 OneToOne (javax.persistence.OneToOne)3 UniqueConstraint (javax.persistence.UniqueConstraint)3 CollectionId (org.hibernate.annotations.CollectionId)3