Search in sources :

Example 61 with MappingException

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

the class NativeQueryImpl method setResultSetMapping.

@Override
public NativeQuery setResultSetMapping(String name) {
    ResultSetMappingDefinition mapping = getProducer().getFactory().getNamedQueryRepository().getResultSetMappingDefinition(name);
    if (mapping == null) {
        throw new MappingException("Unknown SqlResultSetMapping [" + name + "]");
    }
    NativeSQLQueryReturn[] returns = mapping.getQueryReturns();
    queryReturns.addAll(Arrays.asList(returns));
    return this;
}
Also used : NativeSQLQueryReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn) ResultSetMappingDefinition(org.hibernate.engine.ResultSetMappingDefinition) MappingException(org.hibernate.MappingException)

Example 62 with MappingException

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

the class AccessMappingTest method testInconsistentAnnotationPlacement.

@Test
public void testInconsistentAnnotationPlacement() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Course1.class);
    cfg.addAnnotatedClass(Student.class);
    SessionFactory sf = null;
    try {
        sf = cfg.buildSessionFactory(serviceRegistry);
        fail("@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail.");
    } catch (MappingException e) {
    // success
    } finally {
        if (sf != null) {
            sf.close();
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) MappingException(org.hibernate.MappingException) Test(org.junit.Test)

Example 63 with MappingException

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

the class BackquoteTest method testInvalidReferenceToQuotedTableName.

/**
	 *  HHH-4647 : Problems with @JoinColumn referencedColumnName and quoted column and table names
	 *
	 *  An invalid referencedColumnName to an entity having a quoted table name results in an
	 *  infinite loop in o.h.c.Configuration$MappingsImpl#getPhysicalColumnName().
	 *  The same issue exists with getLogicalColumnName()
	 */
@Test
@TestForIssue(jiraKey = "HHH-4647")
public void testInvalidReferenceToQuotedTableName() {
    try {
        Configuration config = new Configuration();
        config.addAnnotatedClass(Printer.class);
        config.addAnnotatedClass(PrinterCable.class);
        sessionFactory = config.buildSessionFactory(serviceRegistry);
        fail("expected MappingException to be thrown");
    }//we WANT MappingException to be thrown
     catch (MappingException e) {
        assertTrue("MappingException was thrown", true);
    } catch (Exception e) {
        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        log.debug(writer.toString());
        fail(e.getMessage());
    } finally {
        if (sessionFactory != null) {
            sessionFactory.close();
            sessionFactory = null;
        }
    }
}
Also used : Configuration(org.hibernate.cfg.Configuration) StringWriter(java.io.StringWriter) MappingException(org.hibernate.MappingException) MappingException(org.hibernate.MappingException) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 64 with MappingException

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

the class PersisterClassProviderTest method testPersisterClassProvider.

@Test
public void testPersisterClassProvider() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Gate.class);
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
    //no exception as the GoofyPersisterClassProvider is not set
    SessionFactory sessionFactory;
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
    cfg = new Configuration();
    cfg.addAnnotatedClass(Gate.class);
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
        fail("The entity persister should be overridden");
    } catch (MappingException e) {
        assertEquals("The entity persister should be overridden", GoofyPersisterClassProvider.NoopEntityPersister.class, ((GoofyException) e.getCause()).getValue());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
    cfg = new Configuration();
    cfg.addAnnotatedClass(Portal.class);
    cfg.addAnnotatedClass(Window.class);
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
        fail("The collection persister should be overridden but not the entity persister");
    } catch (MappingException e) {
        assertEquals("The collection persister should be overridden but not the entity persister", GoofyPersisterClassProvider.NoopCollectionPersister.class, ((GoofyException) e.getCause()).getValue());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    cfg = new Configuration();
    cfg.addAnnotatedClass(Tree.class);
    cfg.addAnnotatedClass(Palmtree.class);
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
        fail("The entity persisters should be overridden in a class hierarchy");
    } catch (MappingException e) {
        assertEquals("The entity persisters should be overridden in a class hierarchy", GoofyPersisterClassProvider.NoopEntityPersister.class, ((GoofyException) e.getCause()).getValue());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ServiceRegistry(org.hibernate.service.ServiceRegistry) MappingException(org.hibernate.MappingException) Test(org.junit.Test)

Example 65 with MappingException

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

the class NamedQueryBinder method processNamedNativeQuery.

public static void processNamedNativeQuery(final HbmLocalMetadataBuildingContext context, JaxbHbmNamedNativeQueryType namedQueryBinding, String prefix) {
    final String queryName = prefix + namedQueryBinding.getName();
    final NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder().setName(queryName).setComment(namedQueryBinding.getComment()).setCacheable(namedQueryBinding.isCacheable()).setCacheMode(namedQueryBinding.getCacheMode()).setCacheRegion(namedQueryBinding.getCacheRegion()).setTimeout(namedQueryBinding.getTimeout()).setReadOnly(namedQueryBinding.isReadOnly()).setFlushMode(namedQueryBinding.getFlushMode()).setFetchSize(namedQueryBinding.getFetchSize()).setCallable(namedQueryBinding.isCallable()).setResultSetRef(namedQueryBinding.getResultsetRef());
    final ImplicitResultSetMappingDefinition.Builder implicitResultSetMappingBuilder = new ImplicitResultSetMappingDefinition.Builder(queryName);
    boolean foundQuery = false;
    for (Object content : namedQueryBinding.getContent()) {
        final boolean wasQuery = processNamedQueryContentItem(content, builder, implicitResultSetMappingBuilder, namedQueryBinding, context);
        if (wasQuery) {
            foundQuery = true;
        }
    }
    if (!foundQuery) {
        throw new org.hibernate.boot.MappingException(String.format("Named native query [%s] did not specify query string", namedQueryBinding.getName()), context.getOrigin());
    }
    if (implicitResultSetMappingBuilder.hasAnyReturns()) {
        if (StringHelper.isNotEmpty(namedQueryBinding.getResultsetRef())) {
            throw new org.hibernate.boot.MappingException(String.format("Named native query [%s] specified both a resultset-ref and an inline mapping of results", namedQueryBinding.getName()), context.getOrigin());
        }
        // Building a ResultSet mapping needs access to entity bindings for any entity
        // returns it defines.  But binding for those entities may have not been
        // completed yet.  For "normal" ResultSet mappings, this is already handled by
        // the fact that MetadataSourceProcessor#processResultSetMappings() is called
        // afterQuery all entity hierarchies have been processed.  However, here we are in
        // the middle of processing named-queries (either top-level or entity-level)
        // and have no guarantee that any entity bindings we may need here are bound.
        // So we add the second-pass to bind the implicit resultSet mapping.
        //
        // It is possible to know here whether the second-pass is needed or whether we
        // can immediately bind the ResultSet mapping.
        // todo : consider implementing this (^^) checking
        final ImplicitResultSetMappingDefinition implicitResultSetMappingDefinition = implicitResultSetMappingBuilder.build();
        builder.setResultSetRef(implicitResultSetMappingDefinition.getName());
        context.getMetadataCollector().addSecondPass(new SecondPass() {

            @Override
            public void doSecondPass(Map persistentClasses) throws MappingException {
                final ResultSetMappingDefinition resultSetMappingDefinition = ResultSetMappingBinder.bind(implicitResultSetMappingDefinition, context);
                context.getMetadataCollector().addResultSetMapping(resultSetMappingDefinition);
                NativeSQLQueryReturn[] newQueryReturns = resultSetMappingDefinition.getQueryReturns();
                final NamedSQLQueryDefinition queryDefinition = context.getMetadataCollector().getNamedNativeQueryDefinition(queryName);
                if (queryDefinition != null) {
                    queryDefinition.addQueryReturns(newQueryReturns);
                }
            }
        });
    }
    context.getMetadataCollector().addNamedNativeQuery(builder.createNamedQueryDefinition());
}
Also used : NamedQueryDefinitionBuilder(org.hibernate.engine.spi.NamedQueryDefinitionBuilder) NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder) ImplicitResultSetMappingDefinition(org.hibernate.boot.jaxb.hbm.internal.ImplicitResultSetMappingDefinition) MappingException(org.hibernate.MappingException) NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) SecondPass(org.hibernate.cfg.SecondPass) HashMap(java.util.HashMap) Map(java.util.Map) NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder) ResultSetMappingDefinition(org.hibernate.engine.ResultSetMappingDefinition) ImplicitResultSetMappingDefinition(org.hibernate.boot.jaxb.hbm.internal.ImplicitResultSetMappingDefinition)

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