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;
}
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();
}
}
}
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;
}
}
}
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());
}
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());
}
Aggregations