Search in sources :

Example 11 with AnnotationException

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

the class AttributeConverterTest method testErrorInstantiatingConverterClass.

@Test
public void testErrorInstantiatingConverterClass() {
    Configuration cfg = new Configuration();
    try {
        cfg.addAttributeConverter(BlowsUpConverter.class);
        fail("expecting an exception");
    } catch (AnnotationException e) {
        assertNotNull(e.getCause());
        assertTyping(BlewUpException.class, e.getCause());
    }
}
Also used : Configuration(org.hibernate.cfg.Configuration) AnnotationException(org.hibernate.AnnotationException) Test(org.junit.Test)

Example 12 with AnnotationException

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

the class InvalidEnumeratedJavaTypeTest method testInvalidMapping.

@Test
public void testInvalidMapping() {
    MetadataSources metadataSources = new MetadataSources().addAnnotatedClass(TheEntity.class);
    try {
        metadataSources.buildMetadata();
        fail("Was expecting failure");
    } catch (AnnotationException ignore) {
    } finally {
        ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();
        if (metaServiceRegistry instanceof BootstrapServiceRegistry) {
            BootstrapServiceRegistryBuilder.destroy(metaServiceRegistry);
        }
    }
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) AnnotationException(org.hibernate.AnnotationException) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) ServiceRegistry(org.hibernate.service.ServiceRegistry) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) Test(org.junit.Test)

Example 13 with AnnotationException

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

the class SpreadNaturalIdTest method testSpreadNaturalIdDeclarationGivesMappingException.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void testSpreadNaturalIdDeclarationGivesMappingException() {
    final MetadataSources metadataSources = new MetadataSources().addAnnotatedClass(Principal.class).addAnnotatedClass(User.class);
    try {
        metadataSources.buildMetadata();
        fail("Expected binders to throw an exception");
    } catch (AnnotationException expected) {
    } finally {
        ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();
        if (metaServiceRegistry instanceof BootstrapServiceRegistry) {
            BootstrapServiceRegistryBuilder.destroy(metaServiceRegistry);
        }
    }
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) AnnotationException(org.hibernate.AnnotationException) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) ServiceRegistry(org.hibernate.service.ServiceRegistry) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) Test(org.junit.Test)

Example 14 with AnnotationException

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

the class InFlightMetadataCollectorImpl method buildRecursiveOrderedFkSecondPasses.

/**
 * Recursively builds a list of FkSecondPass instances ready to be processed in this order.
 * Checking all dependencies recursively seems quite expensive, but the original code just relied
 * on some sort of table name sorting which failed in certain circumstances.
 * <p/>
 * See <tt>ANN-722</tt> and <tt>ANN-730</tt>
 *
 * @param orderedFkSecondPasses The list containing the <code>FkSecondPass<code> instances ready
 * for processing.
 * @param isADependencyOf Our lookup data structure to determine dependencies between tables
 * @param startTable Table name to start recursive algorithm.
 * @param currentTable The current table name used to check for 'new' dependencies.
 */
private void buildRecursiveOrderedFkSecondPasses(List<FkSecondPass> orderedFkSecondPasses, Map<String, Set<FkSecondPass>> isADependencyOf, String startTable, String currentTable) {
    Set<FkSecondPass> dependencies = isADependencyOf.get(currentTable);
    // bottom out
    if (dependencies == null || dependencies.size() == 0) {
        return;
    }
    for (FkSecondPass sp : dependencies) {
        String dependentTable = sp.getValue().getTable().getQualifiedTableName().render();
        if (dependentTable.compareTo(startTable) == 0) {
            throw new AnnotationException("Foreign key circularity dependency involving the following tables: " + startTable + ", " + dependentTable);
        }
        buildRecursiveOrderedFkSecondPasses(orderedFkSecondPasses, isADependencyOf, startTable, dependentTable);
        if (!orderedFkSecondPasses.contains(sp)) {
            orderedFkSecondPasses.add(0, sp);
        }
    }
}
Also used : FkSecondPass(org.hibernate.cfg.FkSecondPass) AnnotationException(org.hibernate.AnnotationException)

Example 15 with AnnotationException

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

the class AnnotationBinder method bindIdClass.

private static void bindIdClass(String generatorType, String generatorName, PropertyData inferredData, PropertyData baseInferredData, Ejb3Column[] columns, PropertyHolder propertyHolder, boolean isComposite, AccessType propertyAccessor, EntityBinder entityBinder, boolean isEmbedded, boolean isIdentifierMapper, MetadataBuildingContext buildingContext, Map<XClass, InheritanceState> inheritanceStatePerClass) {
    /*
		 * Fill simple value and property since and Id is a property
		 */
    PersistentClass persistentClass = propertyHolder.getPersistentClass();
    if (!(persistentClass instanceof RootClass)) {
        throw new AnnotationException("Unable to define/override @Id(s) on a subclass: " + propertyHolder.getEntityName());
    }
    RootClass rootClass = (RootClass) persistentClass;
    String persistentClassName = rootClass.getClassName();
    SimpleValue id;
    final String propertyName = inferredData.getPropertyName();
    if (isComposite) {
        id = fillComponent(propertyHolder, inferredData, baseInferredData, propertyAccessor, false, entityBinder, isEmbedded, isIdentifierMapper, false, buildingContext, inheritanceStatePerClass);
        Component componentId = (Component) id;
        componentId.setKey(true);
        if (rootClass.getIdentifier() != null) {
            throw new AnnotationException(componentId.getComponentClassName() + " must not have @Id properties when used as an @EmbeddedId");
        }
        if (componentId.getPropertySpan() == 0) {
            throw new AnnotationException(componentId.getComponentClassName() + " has no persistent id property");
        }
        // tuplizers
        XProperty property = inferredData.getProperty();
        setupComponentTuplizer(property, componentId);
    } else {
        for (Ejb3Column column : columns) {
            // this is an id
            column.forceNotNull();
        }
        SimpleValueBinder value = new SimpleValueBinder();
        value.setPropertyName(propertyName);
        value.setReturnedClassName(inferredData.getTypeName());
        value.setColumns(columns);
        value.setPersistentClassName(persistentClassName);
        value.setBuildingContext(buildingContext);
        value.setType(inferredData.getProperty(), inferredData.getClassOrElement(), persistentClassName, null);
        value.setAccessType(propertyAccessor);
        id = value.make();
    }
    rootClass.setIdentifier(id);
    SecondPass secondPass = new IdGeneratorResolverSecondPass(id, inferredData.getProperty(), generatorType, generatorName, buildingContext);
    buildingContext.getMetadataCollector().addSecondPass(secondPass);
    if (isEmbedded) {
        rootClass.setEmbeddedIdentifier(inferredData.getPropertyClass() == null);
    } else {
        PropertyBinder binder = new PropertyBinder();
        binder.setName(propertyName);
        binder.setValue(id);
        binder.setAccessType(inferredData.getDefaultAccess());
        binder.setProperty(inferredData.getProperty());
        Property prop = binder.makeProperty();
        rootClass.setIdentifierProperty(prop);
        // if the id property is on a superclass, update the metamodel
        final org.hibernate.mapping.MappedSuperclass superclass = BinderHelper.getMappedSuperclassOrNull(inferredData.getDeclaringClass(), inheritanceStatePerClass, buildingContext);
        if (superclass != null) {
            superclass.setDeclaredIdentifierProperty(prop);
        } else {
            // we know the property is on the actual entity
            rootClass.setDeclaredIdentifierProperty(prop);
        }
    }
}
Also used : RootClass(org.hibernate.mapping.RootClass) XProperty(org.hibernate.annotations.common.reflection.XProperty) SimpleValue(org.hibernate.mapping.SimpleValue) SimpleValueBinder(org.hibernate.cfg.annotations.SimpleValueBinder) AnnotationException(org.hibernate.AnnotationException) PropertyBinder(org.hibernate.cfg.annotations.PropertyBinder) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty) PersistentClass(org.hibernate.mapping.PersistentClass)

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