Search in sources :

Example 11 with HibernateConsoleRuntimeException

use of org.hibernate.console.HibernateConsoleRuntimeException in project jbosstools-hibernate by jbosstools.

the class ServiceImpl method newJpaConfiguration.

@Override
public IConfiguration newJpaConfiguration(String entityResolver, String persistenceUnit, Map<Object, Object> overrides) {
    getUsageTracker().trackNewConfigurationEvent(HIBERNATE_VERSION);
    IConfiguration result = null;
    try {
        HibernatePersistenceProvider hibernatePersistenceProvider = new HibernatePersistenceProvider();
        Method getEntityManagerFactoryBuilderOrNull = hibernatePersistenceProvider.getClass().getDeclaredMethod("getEntityManagerFactoryBuilderOrNull", new Class[] { String.class, Map.class });
        getEntityManagerFactoryBuilderOrNull.setAccessible(true);
        Object entityManagerFactoryBuilder = getEntityManagerFactoryBuilderOrNull.invoke(hibernatePersistenceProvider, new Object[] { persistenceUnit, overrides });
        if (entityManagerFactoryBuilder == null) {
            throw new HibernateConsoleRuntimeException("Persistence unit not found: '" + persistenceUnit + "'.");
        }
        Method buildServiceRegistry = entityManagerFactoryBuilder.getClass().getMethod("buildServiceRegistry", new Class[0]);
        Object serviceRegistry = buildServiceRegistry.invoke(entityManagerFactoryBuilder, (Object[]) null);
        Class<?> serviceRegistryClass = StandardClassLoaderDelegateImpl.INSTANCE.classForName("org.hibernate.service.ServiceRegistry");
        Method buildHibernateConfiguration = entityManagerFactoryBuilder.getClass().getMethod("buildHibernateConfiguration", new Class[] { serviceRegistryClass });
        Configuration configuration = (Configuration) buildHibernateConfiguration.invoke(entityManagerFactoryBuilder, new Object[] { serviceRegistry });
        result = facadeFactory.createConfiguration(configuration);
    } catch (SecurityException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new HibernateConsoleRuntimeException(e);
    }
    return result;
}
Also used : Configuration(org.hibernate.cfg.Configuration) JDBCMetaDataConfiguration(org.hibernate.cfg.JDBCMetaDataConfiguration) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration)

Example 12 with HibernateConsoleRuntimeException

use of org.hibernate.console.HibernateConsoleRuntimeException in project jbosstools-hibernate by jbosstools.

the class ServiceImpl method newJpaConfiguration.

@Override
public IConfiguration newJpaConfiguration(String entityResolver, String persistenceUnit, Map<Object, Object> overrides) {
    getUsageTracker().trackNewConfigurationEvent(HIBERNATE_VERSION);
    Ejb3Configuration ejb3Configuration = new Ejb3Configuration();
    if (StringHelper.isNotEmpty(entityResolver)) {
        try {
            Class<?> resolver = ReflectHelper.classForName(entityResolver, this.getClass());
            Object object = resolver.newInstance();
            ejb3Configuration.setEntityResolver((EntityResolver) object);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new HibernateConsoleRuntimeException(e);
        }
    }
    ejb3Configuration.setProperty("hibernate.validator.autoregister_listeners", "false");
    ejb3Configuration.setProperty("hibernate.validator.apply_to_ddl", "false");
    ejb3Configuration.configure(persistenceUnit, overrides);
    Configuration configuration = ejb3Configuration.getHibernateConfiguration();
    return facadeFactory.createConfiguration(configuration);
}
Also used : Ejb3Configuration(org.hibernate.ejb.Ejb3Configuration) Configuration(org.hibernate.cfg.Configuration) JDBCMetaDataConfiguration(org.hibernate.cfg.JDBCMetaDataConfiguration) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) Ejb3Configuration(org.hibernate.ejb.Ejb3Configuration)

Example 13 with HibernateConsoleRuntimeException

use of org.hibernate.console.HibernateConsoleRuntimeException in project jbosstools-hibernate by jbosstools.

the class ServiceImpl method newReverseEngineeringStrategy.

private Object newReverseEngineeringStrategy(final String className, Object delegate) {
    try {
        Class<?> clazz = ReflectHelper.classForName(className);
        Class<?> revEngClass = ReflectHelper.classForName("org.hibernate.cfg.reveng.ReverseEngineeringStrategy");
        Constructor<?> constructor = clazz.getConstructor(new Class[] { revEngClass });
        return constructor.newInstance(new Object[] { delegate });
    } catch (NoSuchMethodException e) {
        try {
            ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            Class<?> clazz = null;
            if (contextClassLoader != null) {
                clazz = contextClassLoader.loadClass(className);
            } else {
                clazz = Class.forName(className);
            }
            if (clazz != null) {
                return clazz.newInstance();
            } else {
                throw new HibernateConsoleRuntimeException("Class " + className + " could not be found.");
            }
        } catch (Exception eq) {
            throw new HibernateConsoleRuntimeException(eq);
        }
    } catch (Exception e) {
        throw new HibernateConsoleRuntimeException(e);
    }
}
Also used : PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) SQLException(java.sql.SQLException) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException)

Example 14 with HibernateConsoleRuntimeException

use of org.hibernate.console.HibernateConsoleRuntimeException in project jbosstools-hibernate by jbosstools.

the class ServiceImpl method newDialect.

@Override
public String newDialect(Properties properties, final Connection connection) {
    ServiceRegistry serviceRegistry = buildServiceRegistry(properties);
    DialectFactory dialectFactory = serviceRegistry.getService(DialectFactory.class);
    Dialect dialect = dialectFactory.buildDialect(properties, new DialectResolutionInfoSource() {

        @Override
        public DialectResolutionInfo getDialectResolutionInfo() {
            try {
                return new DatabaseMetaDataDialectResolutionInfoAdapter(connection.getMetaData());
            } catch (SQLException sqlException) {
                throw new HibernateConsoleRuntimeException("Unable to access java.sql.DatabaseMetaData to determine appropriate Dialect to use", sqlException);
            }
        }
    });
    return dialect != null ? dialect.toString() : null;
}
Also used : DialectResolutionInfoSource(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource) SQLException(java.sql.SQLException) MetaDataDialect(org.hibernate.cfg.reveng.dialect.MetaDataDialect) Dialect(org.hibernate.dialect.Dialect) DatabaseMetaDataDialectResolutionInfoAdapter(org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter) DialectFactory(org.hibernate.engine.jdbc.dialect.spi.DialectFactory) ServiceRegistry(org.hibernate.service.ServiceRegistry) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) DialectResolutionInfo(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)

Example 15 with HibernateConsoleRuntimeException

use of org.hibernate.console.HibernateConsoleRuntimeException in project jbosstools-hibernate by jbosstools.

the class ConsoleConfigurationTest method testBuildConfiguration.

@Test
public void testBuildConfiguration() {
    MockCCListener listener = new MockCCListener();
    Assert.assertTrue(consoleCfg.getConsoleConfigurationListeners().length == 1);
    consoleCfg.addConsoleConfigurationListener(listener);
    consoleCfg.build();
    Assert.assertEquals(0, listener.factoryBuilt);
    consoleCfg.buildSessionFactory();
    Assert.assertEquals(1, listener.factoryBuilt);
    try {
        consoleCfg.buildSessionFactory();
        Assert.fail(TestConsoleMessages.ConsoleConfigurationTest_factory_already_exists);
    } catch (HibernateConsoleRuntimeException hcre) {
    }
    // $NON-NLS-1$
    QueryPage qp = consoleCfg.executeHQLQuery("from java.lang.Object");
    Assert.assertNotNull(qp);
    Assert.assertEquals(1, listener.queryCreated);
    consoleCfg.closeSessionFactory();
    Assert.assertEquals(1, listener.factoryClosing);
}
Also used : QueryPage(org.hibernate.console.QueryPage) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) Test(org.junit.Test)

Aggregations

HibernateConsoleRuntimeException (org.hibernate.console.HibernateConsoleRuntimeException)34 SQLException (java.sql.SQLException)10 File (java.io.File)8 CoreException (org.eclipse.core.runtime.CoreException)7 PersistentClass (org.hibernate.mapping.PersistentClass)7 RootClass (org.hibernate.mapping.RootClass)7 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)7 Properties (java.util.Properties)5 MetaDataDialect (org.hibernate.cfg.reveng.dialect.MetaDataDialect)5 ConsoleConfiguration (org.hibernate.console.ConsoleConfiguration)5 Dialect (org.hibernate.dialect.Dialect)5 DatabaseMetaDataDialectResolutionInfoAdapter (org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter)5 DialectFactory (org.hibernate.engine.jdbc.dialect.spi.DialectFactory)5 DialectResolutionInfo (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)5 DialectResolutionInfoSource (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource)5 ServiceRegistry (org.hibernate.service.ServiceRegistry)5 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)5 IOException (java.io.IOException)4 Configuration (org.hibernate.cfg.Configuration)3 JDBCMetaDataConfiguration (org.hibernate.cfg.JDBCMetaDataConfiguration)3