Search in sources :

Example 51 with MappingException

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

the class QueryTranslatorTestCase method assertTranslation.

protected void assertTranslation(String hql, Map replacements, boolean scalar, String sql) {
    SessionFactoryImplementor factory = sessionFactory();
    // Create an empty replacements map if we don't have one.
    if (replacements == null) {
        replacements = new HashMap();
    }
    // steve -> note that the empty maps here represent the currently enabled filters...
    QueryTranslator oldQueryTranslator = null;
    Exception oldException = null;
    try {
        System.out.println("Compiling with classic QueryTranslator...");
        QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
        oldQueryTranslator = classic.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
        oldQueryTranslator.compile(replacements, scalar);
    } catch (QueryException e) {
        oldException = e;
    } catch (MappingException e) {
        oldException = e;
    }
    QueryTranslator newQueryTranslator = null;
    Exception newException = null;
    try {
        System.out.println("Compiling with AST QueryTranslator...");
        newQueryTranslator = createNewQueryTranslator(hql, replacements, scalar);
    } catch (QueryException e) {
        newException = e;
    } catch (MappingException e) {
        newException = e;
    }
    // If the old QT threw an exception, the new one should too.
    if (oldException != null) {
        assertNotNull("New query translator did *NOT* throw an exception, the old one did : " + oldException, newException);
        assertEquals(oldException.getMessage(), newException.getMessage());
        // Don't bother with the rest of the assertions.
        return;
    } else if (newException != null) {
        newException.printStackTrace();
        assertNull("Old query translator did not throw an exception, the new one did", newException);
    }
    // -- check all of the outputs --
    checkSql(oldQueryTranslator, newQueryTranslator, hql, scalar, sql);
    checkQuerySpaces(oldQueryTranslator, newQueryTranslator);
    checkReturnedTypes(oldQueryTranslator, newQueryTranslator);
}
Also used : ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) QueryException(org.hibernate.QueryException) HashMap(java.util.HashMap) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) MappingException(org.hibernate.MappingException) QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException)

Example 52 with MappingException

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

the class QueryTranslatorTestCase method compileBadHql.

protected Exception compileBadHql(String hql, boolean scalar) {
    QueryTranslator newQueryTranslator;
    Map replacements = null;
    Exception newException = null;
    SessionFactoryImplementor factory = sessionFactory();
    try {
        QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
        newQueryTranslator = ast.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
        newQueryTranslator.compile(replacements, scalar);
    } catch (QueryException e) {
        newException = e;
    } catch (MappingException e) {
        newException = e;
    }
    assertNotNull("Expected exception from compilation of '" + hql + "'!", newException);
    return newException;
}
Also used : ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) QueryException(org.hibernate.QueryException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) MappingException(org.hibernate.MappingException) QueryException(org.hibernate.QueryException) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) MappingException(org.hibernate.MappingException)

Example 53 with MappingException

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

the class AnnotationBinder method processJoinedDiscriminatorProperties.

/**
	 * Process all discriminator-related metadata per rules for "joined" inheritance
	 */
private static Ejb3DiscriminatorColumn processJoinedDiscriminatorProperties(XClass clazzToProcess, MetadataBuildingContext context, InheritanceState inheritanceState, EntityBinder entityBinder) {
    if (clazzToProcess.isAnnotationPresent(DiscriminatorFormula.class)) {
        throw new MappingException("@DiscriminatorFormula on joined inheritance not supported at this time");
    }
    // DiscriminatorValue handling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final DiscriminatorValue discriminatorValueAnnotation = clazzToProcess.getAnnotation(DiscriminatorValue.class);
    final String discriminatorValue = discriminatorValueAnnotation != null ? clazzToProcess.getAnnotation(DiscriminatorValue.class).value() : null;
    entityBinder.setDiscriminatorValue(discriminatorValue);
    // DiscriminatorColumn handling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final DiscriminatorColumn discriminatorColumnAnnotation = clazzToProcess.getAnnotation(DiscriminatorColumn.class);
    if (!inheritanceState.hasParents()) {
        // we want to process the discriminator column if either:
        //		1) There is an explicit DiscriminatorColumn annotation && we are not told to ignore them
        //		2) There is not an explicit DiscriminatorColumn annotation && we are told to create them implicitly
        final boolean generateDiscriminatorColumn;
        if (discriminatorColumnAnnotation != null) {
            if (context.getBuildingOptions().ignoreExplicitDiscriminatorsForJoinedInheritance()) {
                LOG.debugf("Ignoring explicit DiscriminatorColumn annotation on ", clazzToProcess.getName());
                generateDiscriminatorColumn = false;
            } else {
                LOG.applyingExplicitDiscriminatorColumnForJoined(clazzToProcess.getName(), AvailableSettings.IGNORE_EXPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS);
                generateDiscriminatorColumn = true;
            }
        } else {
            if (context.getBuildingOptions().createImplicitDiscriminatorsForJoinedInheritance()) {
                LOG.debug("Applying implicit DiscriminatorColumn using DiscriminatorColumn defaults");
                generateDiscriminatorColumn = true;
            } else {
                LOG.debug("Ignoring implicit (absent) DiscriminatorColumn");
                generateDiscriminatorColumn = false;
            }
        }
        if (generateDiscriminatorColumn) {
            final DiscriminatorType discriminatorType = discriminatorColumnAnnotation != null ? discriminatorColumnAnnotation.discriminatorType() : DiscriminatorType.STRING;
            return Ejb3DiscriminatorColumn.buildDiscriminatorColumn(discriminatorType, discriminatorColumnAnnotation, null, context);
        }
    } else {
        if (discriminatorColumnAnnotation != null) {
            LOG.invalidDiscriminatorAnnotation(clazzToProcess.getName());
        }
    }
    return null;
}
Also used : DiscriminatorType(javax.persistence.DiscriminatorType) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) DiscriminatorValue(javax.persistence.DiscriminatorValue) MappingException(org.hibernate.MappingException)

Example 54 with MappingException

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

the class SessionFactoryImpl method resolveParameterBindType.

@Override
public Type resolveParameterBindType(Class clazz) {
    String typename = clazz.getName();
    Type type = getTypeResolver().heuristicType(typename);
    boolean serializable = type != null && type instanceof SerializableType;
    if (type == null || serializable) {
        try {
            getMetamodel().entityPersister(clazz.getName());
        } catch (MappingException me) {
            if (serializable) {
                return type;
            } else {
                throw new HibernateException("Could not determine a type for class: " + typename);
            }
        }
        return getTypeHelper().entity(clazz);
    } else {
        return type;
    }
}
Also used : SerializableType(org.hibernate.type.SerializableType) SynchronizationType(javax.persistence.SynchronizationType) EventType(org.hibernate.event.spi.EventType) PersistenceContextType(javax.persistence.PersistenceContextType) PersistenceUnitTransactionType(javax.persistence.spi.PersistenceUnitTransactionType) Type(org.hibernate.type.Type) SerializableType(org.hibernate.type.SerializableType) HibernateException(org.hibernate.HibernateException) MappingException(org.hibernate.MappingException)

Example 55 with MappingException

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

the class DefaultIdentifierGeneratorFactory method createIdentifierGenerator.

@Override
public IdentifierGenerator createIdentifierGenerator(String strategy, Type type, Properties config) {
    try {
        Class clazz = getIdentifierGeneratorClass(strategy);
        IdentifierGenerator identifierGenerator = (IdentifierGenerator) clazz.newInstance();
        if (identifierGenerator instanceof Configurable) {
            ((Configurable) identifierGenerator).configure(type, config, serviceRegistry);
        }
        return identifierGenerator;
    } catch (Exception e) {
        final String entityName = config.getProperty(IdentifierGenerator.ENTITY_NAME);
        throw new MappingException(String.format("Could not instantiate id generator [entity-name=%s]", entityName), e);
    }
}
Also used : Configurable(org.hibernate.id.Configurable) MappingException(org.hibernate.MappingException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) MappingException(org.hibernate.MappingException)

Aggregations

MappingException (org.hibernate.MappingException)94 PersistentClass (org.hibernate.mapping.PersistentClass)17 HibernateException (org.hibernate.HibernateException)12 Iterator (java.util.Iterator)11 Test (org.junit.Test)11 AnnotationException (org.hibernate.AnnotationException)10 QueryException (org.hibernate.QueryException)10 Type (org.hibernate.type.Type)10 Property (org.hibernate.mapping.Property)9 HashMap (java.util.HashMap)8 XClass (org.hibernate.annotations.common.reflection.XClass)8 DuplicateMappingException (org.hibernate.DuplicateMappingException)6 Configuration (org.hibernate.cfg.Configuration)6 UnknownSqlResultSetMappingException (org.hibernate.procedure.UnknownSqlResultSetMappingException)6 ServiceRegistry (org.hibernate.service.ServiceRegistry)6 Map (java.util.Map)5 AssociationType (org.hibernate.type.AssociationType)5 HashSet (java.util.HashSet)4 ClassLoadingException (org.hibernate.annotations.common.reflection.ClassLoadingException)4 MetadataSources (org.hibernate.boot.MetadataSources)4