Search in sources :

Example 21 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class XMLContext method setLocalAttributeConverterDefinitions.

@SuppressWarnings("unchecked")
private void setLocalAttributeConverterDefinitions(List<Element> converterElements) {
    for (Element converterElement : converterElements) {
        final String className = converterElement.attributeValue("class");
        final String autoApplyAttribute = converterElement.attributeValue("auto-apply");
        final boolean autoApply = autoApplyAttribute != null && Boolean.parseBoolean(autoApplyAttribute);
        try {
            final Class<? extends AttributeConverter> attributeConverterClass = classLoaderAccess.classForName(className);
            attributeConverterInfoList.add(new AttributeConverterDefinition(attributeConverterClass.newInstance(), autoApply));
        } catch (ClassLoadingException e) {
            throw new AnnotationException("Unable to locate specified AttributeConverter implementation class : " + className, e);
        } catch (Exception e) {
            throw new AnnotationException("Unable to instantiate specified AttributeConverter implementation class : " + className, e);
        }
    }
}
Also used : ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Element(org.dom4j.Element) AttributeConverterDefinition(org.hibernate.cfg.AttributeConverterDefinition) AnnotationException(org.hibernate.AnnotationException) AnnotationException(org.hibernate.AnnotationException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException)

Example 22 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class ToOneFkSecondPass method doSecondPass.

public void doSecondPass(java.util.Map persistentClasses) throws MappingException {
    if (value instanceof ManyToOne) {
        ManyToOne manyToOne = (ManyToOne) value;
        PersistentClass ref = (PersistentClass) persistentClasses.get(manyToOne.getReferencedEntityName());
        if (ref == null) {
            throw new AnnotationException("@OneToOne or @ManyToOne on " + StringHelper.qualify(entityClassName, path) + " references an unknown entity: " + manyToOne.getReferencedEntityName());
        }
        manyToOne.setPropertyName(path);
        BinderHelper.createSyntheticPropertyReference(columns, ref, null, manyToOne, false, buildingContext);
        TableBinder.bindFk(ref, null, columns, manyToOne, unique, buildingContext);
        /*
			 * HbmMetadataSourceProcessorImpl does this only when property-ref != null, but IMO, it makes sense event if it is null
			 */
        if (!manyToOne.isIgnoreNotFound())
            manyToOne.createPropertyRefConstraints(persistentClasses);
    } else if (value instanceof OneToOne) {
        value.createForeignKey();
    } else {
        throw new AssertionFailure("FkSecondPass for a wrong value type: " + value.getClass().getName());
    }
}
Also used : OneToOne(org.hibernate.mapping.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) AnnotationException(org.hibernate.AnnotationException) ManyToOne(org.hibernate.mapping.ManyToOne) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 23 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class CollectionBinder method getCollectionBinder.

/**
 * collection binder factory
 */
public static CollectionBinder getCollectionBinder(String entityName, XProperty property, boolean isIndexed, boolean isHibernateExtensionMapping, MetadataBuildingContext buildingContext) {
    final CollectionBinder result;
    if (property.isArray()) {
        if (property.getElementClass().isPrimitive()) {
            result = new PrimitiveArrayBinder();
        } else {
            result = new ArrayBinder();
        }
    } else if (property.isCollection()) {
        // TODO consider using an XClass
        Class returnedClass = property.getCollectionClass();
        if (java.util.Set.class.equals(returnedClass)) {
            if (property.isAnnotationPresent(CollectionId.class)) {
                throw new AnnotationException("Set do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
            }
            result = new SetBinder(false);
        } else if (java.util.SortedSet.class.equals(returnedClass)) {
            if (property.isAnnotationPresent(CollectionId.class)) {
                throw new AnnotationException("Set do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
            }
            result = new SetBinder(true);
        } else if (java.util.Map.class.equals(returnedClass)) {
            if (property.isAnnotationPresent(CollectionId.class)) {
                throw new AnnotationException("Map do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
            }
            result = new MapBinder(false);
        } else if (java.util.SortedMap.class.equals(returnedClass)) {
            if (property.isAnnotationPresent(CollectionId.class)) {
                throw new AnnotationException("Map do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
            }
            result = new MapBinder(true);
        } else if (java.util.Collection.class.equals(returnedClass)) {
            if (property.isAnnotationPresent(CollectionId.class)) {
                result = new IdBagBinder();
            } else {
                result = new BagBinder();
            }
        } else if (java.util.List.class.equals(returnedClass)) {
            if (isIndexed) {
                if (property.isAnnotationPresent(CollectionId.class)) {
                    throw new AnnotationException("List do not support @CollectionId and @OrderColumn (or @IndexColumn) at the same time: " + StringHelper.qualify(entityName, property.getName()));
                }
                result = new ListBinder();
            } else if (property.isAnnotationPresent(CollectionId.class)) {
                result = new IdBagBinder();
            } else {
                result = new BagBinder();
            }
        } else {
            throw new AnnotationException(returnedClass.getName() + " collection not yet supported: " + StringHelper.qualify(entityName, property.getName()));
        }
    } else {
        throw new AnnotationException("Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: " + StringHelper.qualify(entityName, property.getName()));
    }
    result.setIsHibernateExtensionMapping(isHibernateExtensionMapping);
    final CollectionType typeAnnotation = property.getAnnotation(CollectionType.class);
    if (typeAnnotation != null) {
        final String typeName = typeAnnotation.type();
        // see if it names a type-def
        final TypeDefinition typeDef = buildingContext.getMetadataCollector().getTypeDefinition(typeName);
        if (typeDef != null) {
            result.explicitType = typeDef.getTypeImplementorClass().getName();
            result.explicitTypeParameters.putAll(typeDef.getParameters());
        } else {
            result.explicitType = typeName;
            for (Parameter param : typeAnnotation.parameters()) {
                result.explicitTypeParameters.setProperty(param.name(), param.value());
            }
        }
    }
    return result;
}
Also used : TypeDefinition(org.hibernate.boot.model.TypeDefinition) CollectionId(org.hibernate.annotations.CollectionId) CollectionType(org.hibernate.annotations.CollectionType) AnnotationException(org.hibernate.AnnotationException) LazyCollection(org.hibernate.annotations.LazyCollection) Collection(org.hibernate.mapping.Collection) ElementCollection(javax.persistence.ElementCollection) Parameter(org.hibernate.annotations.Parameter) PersistentClass(org.hibernate.mapping.PersistentClass) XClass(org.hibernate.annotations.common.reflection.XClass) Map(java.util.Map) BinderHelper.toAliasTableMap(org.hibernate.cfg.BinderHelper.toAliasTableMap) HashMap(java.util.HashMap) BinderHelper.toAliasEntityMap(org.hibernate.cfg.BinderHelper.toAliasEntityMap)

Example 24 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class CollectionBinder method bindFilters.

private void bindFilters(boolean hasAssociationTable) {
    Filter simpleFilter = property.getAnnotation(Filter.class);
    // if ( StringHelper.isNotEmpty( where ) ) collection.setWhere( where );
    if (simpleFilter != null) {
        if (hasAssociationTable) {
            collection.addManyToManyFilter(simpleFilter.name(), getCondition(simpleFilter), simpleFilter.deduceAliasInjectionPoints(), toAliasTableMap(simpleFilter.aliases()), toAliasEntityMap(simpleFilter.aliases()));
        } else {
            collection.addFilter(simpleFilter.name(), getCondition(simpleFilter), simpleFilter.deduceAliasInjectionPoints(), toAliasTableMap(simpleFilter.aliases()), toAliasEntityMap(simpleFilter.aliases()));
        }
    }
    Filters filters = property.getAnnotation(Filters.class);
    if (filters != null) {
        for (Filter filter : filters.value()) {
            if (hasAssociationTable) {
                collection.addManyToManyFilter(filter.name(), getCondition(filter), filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
            } else {
                collection.addFilter(filter.name(), getCondition(filter), filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
            }
        }
    }
    FilterJoinTable simpleFilterJoinTable = property.getAnnotation(FilterJoinTable.class);
    if (simpleFilterJoinTable != null) {
        if (hasAssociationTable) {
            collection.addFilter(simpleFilterJoinTable.name(), simpleFilterJoinTable.condition(), simpleFilterJoinTable.deduceAliasInjectionPoints(), toAliasTableMap(simpleFilterJoinTable.aliases()), toAliasEntityMap(simpleFilterJoinTable.aliases()));
        } else {
            throw new AnnotationException("Illegal use of @FilterJoinTable on an association without join table:" + StringHelper.qualify(propertyHolder.getPath(), propertyName));
        }
    }
    FilterJoinTables filterJoinTables = property.getAnnotation(FilterJoinTables.class);
    if (filterJoinTables != null) {
        for (FilterJoinTable filter : filterJoinTables.value()) {
            if (hasAssociationTable) {
                collection.addFilter(filter.name(), filter.condition(), filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
            } else {
                throw new AnnotationException("Illegal use of @FilterJoinTable on an association without join table:" + StringHelper.qualify(propertyHolder.getPath(), propertyName));
            }
        }
    }
    StringBuilder whereBuffer = new StringBuilder();
    if (property.getElementClass() != null) {
        Where whereOnClass = property.getElementClass().getAnnotation(Where.class);
        if (whereOnClass != null) {
            String clause = whereOnClass.clause();
            if (StringHelper.isNotEmpty(clause)) {
                whereBuffer.append(clause);
            }
        }
    }
    Where whereOnCollection = property.getAnnotation(Where.class);
    if (whereOnCollection != null) {
        String clause = whereOnCollection.clause();
        if (StringHelper.isNotEmpty(clause)) {
            if (whereBuffer.length() > 0) {
                whereBuffer.append(' ');
                whereBuffer.append(Junction.Nature.AND.getOperator());
                whereBuffer.append(' ');
            }
            whereBuffer.append(clause);
        }
    }
    if (whereBuffer.length() > 0) {
        String whereClause = whereBuffer.toString();
        if (hasAssociationTable) {
            collection.setManyToManyWhere(whereClause);
        } else {
            collection.setWhere(whereClause);
        }
    }
    WhereJoinTable whereJoinTable = property.getAnnotation(WhereJoinTable.class);
    String whereJoinTableClause = whereJoinTable == null ? null : whereJoinTable.clause();
    if (StringHelper.isNotEmpty(whereJoinTableClause)) {
        if (hasAssociationTable) {
            collection.setWhere(whereJoinTableClause);
        } else {
            throw new AnnotationException("Illegal use of @WhereJoinTable on an association without join table:" + StringHelper.qualify(propertyHolder.getPath(), propertyName));
        }
    }
// This cannot happen in annotations since the second fetch is hardcoded to join
// if ( ( ! collection.getManyToManyFilterMap().isEmpty() || collection.getManyToManyWhere() != null ) &&
// collection.getFetchMode() == FetchMode.JOIN &&
// collection.getElement().getFetchMode() != FetchMode.JOIN ) {
// throw new MappingException(
// "association with join table  defining filter or where without join fetching " +
// "not valid within collection using join fetching [" + collection.getRole() + "]"
// );
// }
}
Also used : WhereJoinTable(org.hibernate.annotations.WhereJoinTable) Filters(org.hibernate.annotations.Filters) Filter(org.hibernate.annotations.Filter) AnnotationException(org.hibernate.AnnotationException) FilterJoinTable(org.hibernate.annotations.FilterJoinTable) FilterJoinTables(org.hibernate.annotations.FilterJoinTables) Where(org.hibernate.annotations.Where)

Example 25 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class CollectionBinder method bindStarToManySecondPass.

/**
 * return true if it's a Fk, false if it's an association table
 */
protected boolean bindStarToManySecondPass(Map persistentClasses, XClass collType, Ejb3JoinColumn[] fkJoinColumns, Ejb3JoinColumn[] keyColumns, Ejb3JoinColumn[] inverseColumns, Ejb3Column[] elementColumns, boolean isEmbedded, XProperty property, boolean unique, TableBinder associationTableBinder, boolean ignoreNotFound, MetadataBuildingContext buildingContext) {
    PersistentClass persistentClass = (PersistentClass) persistentClasses.get(collType.getName());
    boolean reversePropertyInJoin = false;
    if (persistentClass != null && StringHelper.isNotEmpty(this.mappedBy)) {
        try {
            reversePropertyInJoin = 0 != persistentClass.getJoinNumber(persistentClass.getRecursiveProperty(this.mappedBy));
        } catch (MappingException e) {
            throw new AnnotationException("mappedBy reference an unknown target entity property: " + collType + "." + this.mappedBy + " in " + collection.getOwnerEntityName() + "." + property.getName());
        }
    }
    if (persistentClass != null && !reversePropertyInJoin && oneToMany && !this.isExplicitAssociationTable && (// implicit @JoinColumn
    joinColumns[0].isImplicit() && !BinderHelper.isEmptyAnnotationValue(this.mappedBy) || // this is an explicit @JoinColumn
    !fkJoinColumns[0].isImplicit())) {
        // this is a Foreign key
        bindOneToManySecondPass(getCollection(), persistentClasses, fkJoinColumns, collType, cascadeDeleteEnabled, ignoreNotFound, buildingContext, inheritanceStatePerClass);
        return true;
    } else {
        // this is an association table
        bindManyToManySecondPass(this.collection, persistentClasses, keyColumns, inverseColumns, elementColumns, isEmbedded, collType, ignoreNotFound, unique, cascadeDeleteEnabled, associationTableBinder, property, propertyHolder, buildingContext);
        return false;
    }
}
Also used : AnnotationException(org.hibernate.AnnotationException) PersistentClass(org.hibernate.mapping.PersistentClass) MappingException(org.hibernate.MappingException)

Aggregations

AnnotationException (org.hibernate.AnnotationException)85 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)15 PersistentClass (org.hibernate.mapping.PersistentClass)14 AnnotatedElement (java.lang.reflect.AnnotatedElement)12 Element (org.dom4j.Element)12 XClass (org.hibernate.annotations.common.reflection.XClass)12 HashMap (java.util.HashMap)11 MappingException (org.hibernate.MappingException)11 XProperty (org.hibernate.annotations.common.reflection.XProperty)11 Property (org.hibernate.mapping.Property)11 SimpleValue (org.hibernate.mapping.SimpleValue)11 Test (org.junit.Test)10 AssertionFailure (org.hibernate.AssertionFailure)9 ArrayList (java.util.ArrayList)8 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)8 Column (org.hibernate.mapping.Column)8 IdClass (javax.persistence.IdClass)7 MapKeyClass (javax.persistence.MapKeyClass)7 Component (org.hibernate.mapping.Component)7 Properties (java.util.Properties)6