Search in sources :

Example 61 with AnnotationException

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

the class JPAOverriddenAnnotationReader method getAccessType.

private Access getAccessType(Element tree, XMLContext.Default defaults) {
    String access = tree == null ? null : tree.attributeValue("access");
    if (access != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(Access.class);
        AccessType type;
        try {
            type = AccessType.valueOf(access);
        } catch (IllegalArgumentException e) {
            throw new AnnotationException(access + " is not a valid access type. Check you xml confguration.");
        }
        ad.setValue("value", type);
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations() && isPhysicalAnnotationPresent(Access.class)) {
        return getPhysicalAnnotation(Access.class);
    } else if (defaults.getAccess() != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(Access.class);
        ad.setValue("value", defaults.getAccess());
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ClassLoaderAccess(org.hibernate.boot.spi.ClassLoaderAccess) Access(javax.persistence.Access) AnnotationException(org.hibernate.AnnotationException) AccessType(javax.persistence.AccessType)

Example 62 with AnnotationException

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

the class JPAOverriddenAnnotationReader method applyXmlDefinedConverts.

private void applyXmlDefinedConverts(Element containingElement, XMLContext.Default defaults, String attributeNamePrefix, Map<String, Convert> convertAnnotationsMap) {
    final List<Element> convertElements = containingElement.elements("convert");
    for (Element convertElement : convertElements) {
        final AnnotationDescriptor convertAnnotationDescriptor = new AnnotationDescriptor(Convert.class);
        copyStringAttribute(convertAnnotationDescriptor, convertElement, "attribute-name", false);
        copyBooleanAttribute(convertAnnotationDescriptor, convertElement, "disable-conversion");
        final Attribute converterClassAttr = convertElement.attribute("converter");
        if (converterClassAttr != null) {
            final String converterClassName = XMLContext.buildSafeClassName(converterClassAttr.getValue(), defaults);
            try {
                final Class converterClass = classLoaderAccess.classForName(converterClassName);
                convertAnnotationDescriptor.setValue("converter", converterClass);
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find specified converter class id-class: " + converterClassName, e);
            }
        }
        final Convert convertAnnotation = AnnotationFactory.create(convertAnnotationDescriptor);
        final String qualifiedAttributeName = qualifyConverterAttributeName(attributeNamePrefix, convertAnnotation.attributeName());
        convertAnnotationsMap.put(qualifiedAttributeName, convertAnnotation);
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) Convert(javax.persistence.Convert) Attribute(org.dom4j.Attribute) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) AnnotationException(org.hibernate.AnnotationException) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass)

Example 63 with AnnotationException

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

the class InFlightMetadataCollectorImpl method buildUniqueKeyFromColumnNames.

private void buildUniqueKeyFromColumnNames(final Table table, String keyName, final String[] columnNames, String[] orderings, boolean unique, final MetadataBuildingContext buildingContext) {
    int size = columnNames.length;
    Column[] columns = new Column[size];
    Set<Column> unbound = new HashSet<>();
    Set<Column> unboundNoLogical = new HashSet<>();
    for (int index = 0; index < size; index++) {
        final String logicalColumnName = columnNames[index];
        try {
            final String physicalColumnName = getPhysicalColumnName(table, logicalColumnName);
            columns[index] = new Column(physicalColumnName);
            unbound.add(columns[index]);
        // column equals and hashcode is based on column name
        } catch (MappingException e) {
            // If at least 1 columnName does exist, 'columns' will contain a mix of Columns and nulls.  In order
            // to exhaustively report all of the unbound columns at once, w/o an NPE in
            // Constraint#generateName's array sorting, simply create a fake Column.
            columns[index] = new Column(logicalColumnName);
            unboundNoLogical.add(columns[index]);
        }
    }
    final String originalKeyName = keyName;
    if (unique) {
        final Identifier keyNameIdentifier = getMetadataBuildingOptions().getImplicitNamingStrategy().determineUniqueKeyName(new ImplicitUniqueKeyNameSource() {

            @Override
            public MetadataBuildingContext getBuildingContext() {
                return buildingContext;
            }

            @Override
            public Identifier getTableName() {
                return table.getNameIdentifier();
            }

            private List<Identifier> columnNameIdentifiers;

            @Override
            public List<Identifier> getColumnNames() {
                // be lazy about building these
                if (columnNameIdentifiers == null) {
                    columnNameIdentifiers = toIdentifiers(columnNames);
                }
                return columnNameIdentifiers;
            }

            @Override
            public Identifier getUserProvidedIdentifier() {
                return originalKeyName != null ? Identifier.toIdentifier(originalKeyName) : null;
            }
        });
        keyName = keyNameIdentifier.render(getDatabase().getJdbcEnvironment().getDialect());
        UniqueKey uk = table.getOrCreateUniqueKey(keyName);
        for (int i = 0; i < columns.length; i++) {
            Column column = columns[i];
            String order = orderings != null ? orderings[i] : null;
            if (table.containsColumn(column)) {
                uk.addColumn(column, order);
                unbound.remove(column);
            }
        }
    } else {
        final Identifier keyNameIdentifier = getMetadataBuildingOptions().getImplicitNamingStrategy().determineIndexName(new ImplicitIndexNameSource() {

            @Override
            public MetadataBuildingContext getBuildingContext() {
                return buildingContext;
            }

            @Override
            public Identifier getTableName() {
                return table.getNameIdentifier();
            }

            private List<Identifier> columnNameIdentifiers;

            @Override
            public List<Identifier> getColumnNames() {
                // be lazy about building these
                if (columnNameIdentifiers == null) {
                    columnNameIdentifiers = toIdentifiers(columnNames);
                }
                return columnNameIdentifiers;
            }

            @Override
            public Identifier getUserProvidedIdentifier() {
                return originalKeyName != null ? Identifier.toIdentifier(originalKeyName) : null;
            }
        });
        keyName = keyNameIdentifier.render(getDatabase().getJdbcEnvironment().getDialect());
        Index index = table.getOrCreateIndex(keyName);
        for (int i = 0; i < columns.length; i++) {
            Column column = columns[i];
            String order = orderings != null ? orderings[i] : null;
            if (table.containsColumn(column)) {
                index.addColumn(column, order);
                unbound.remove(column);
            }
        }
    }
    if (unbound.size() > 0 || unboundNoLogical.size() > 0) {
        StringBuilder sb = new StringBuilder("Unable to create ");
        if (unique) {
            sb.append("unique key constraint (");
        } else {
            sb.append("index (");
        }
        for (String columnName : columnNames) {
            sb.append(columnName).append(", ");
        }
        sb.setLength(sb.length() - 2);
        sb.append(") on table ").append(table.getName()).append(": database column ");
        for (Column column : unbound) {
            sb.append("'").append(column.getName()).append("', ");
        }
        for (Column column : unboundNoLogical) {
            sb.append("'").append(column.getName()).append("', ");
        }
        sb.setLength(sb.length() - 2);
        sb.append(" not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)");
        throw new AnnotationException(sb.toString());
    }
}
Also used : MetadataBuildingContext(org.hibernate.boot.spi.MetadataBuildingContext) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) Index(org.hibernate.mapping.Index) DuplicateMappingException(org.hibernate.DuplicateMappingException) MappingException(org.hibernate.MappingException) Identifier(org.hibernate.boot.model.naming.Identifier) ImplicitIndexNameSource(org.hibernate.boot.model.naming.ImplicitIndexNameSource) Column(org.hibernate.mapping.Column) UniqueKey(org.hibernate.mapping.UniqueKey) AnnotationException(org.hibernate.AnnotationException) ArrayList(java.util.ArrayList) List(java.util.List) ImplicitUniqueKeyNameSource(org.hibernate.boot.model.naming.ImplicitUniqueKeyNameSource) HashSet(java.util.HashSet)

Example 64 with AnnotationException

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

the class JPAOverriddenAnnotationReader method buildQueryHints.

private static void buildQueryHints(List<Element> elements, AnnotationDescriptor ann) {
    List<QueryHint> queryHints = new ArrayList<>(elements.size());
    for (Element hint : elements) {
        AnnotationDescriptor hintDescriptor = new AnnotationDescriptor(QueryHint.class);
        String value = hint.attributeValue("name");
        if (value == null) {
            throw new AnnotationException("<hint> without name. " + SCHEMA_VALIDATION);
        }
        hintDescriptor.setValue("name", value);
        value = hint.attributeValue("value");
        if (value == null) {
            throw new AnnotationException("<hint> without value. " + SCHEMA_VALIDATION);
        }
        hintDescriptor.setValue("value", value);
        queryHints.add(AnnotationFactory.create(hintDescriptor));
    }
    ann.setValue("hints", queryHints.toArray(new QueryHint[queryHints.size()]));
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) QueryHint(javax.persistence.QueryHint) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) AnnotationException(org.hibernate.AnnotationException)

Example 65 with AnnotationException

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

the class JPAOverriddenAnnotationReader method bindNamedSubgraph.

private static void bindNamedSubgraph(XMLContext.Default defaults, AnnotationDescriptor ann, List<Element> subgraphNodes, ClassLoaderAccess classLoaderAccess) {
    List<NamedSubgraph> annSubgraphNodes = new ArrayList<>();
    for (Element subgraphNode : subgraphNodes) {
        AnnotationDescriptor annSubgraphNode = new AnnotationDescriptor(NamedSubgraph.class);
        copyStringAttribute(annSubgraphNode, subgraphNode, "name", true);
        String clazzName = subgraphNode.attributeValue("class");
        Class clazz;
        try {
            clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
        } catch (ClassLoadingException e) {
            throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
        }
        annSubgraphNode.setValue("type", clazz);
        bindNamedAttributeNodes(subgraphNode, annSubgraphNode);
        annSubgraphNodes.add(AnnotationFactory.create(annSubgraphNode));
    }
    ann.setValue("subgraphs", annSubgraphNodes.toArray(new NamedSubgraph[annSubgraphNodes.size()]));
}
Also used : NamedSubgraph(javax.persistence.NamedSubgraph) AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) AnnotationException(org.hibernate.AnnotationException) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass)

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