Search in sources :

Example 1 with HibernateException

use of org.jboss.tools.hibernate.runtime.spi.HibernateException in project jbosstools-hibernate by jbosstools.

the class LazyDatabaseSchemaWorkbenchAdapter method readDatabaseSchema.

protected IDatabaseCollector readDatabaseSchema(final IProgressMonitor monitor, final ConsoleConfiguration consoleConfiguration, final IReverseEngineeringStrategy strategy) {
    final IConfiguration configuration = consoleConfiguration.buildWith(null, false);
    return (IDatabaseCollector) consoleConfiguration.execute(new ExecutionContext.Command() {

        public Object execute() {
            IDatabaseCollector db = null;
            try {
                IService service = consoleConfiguration.getHibernateExtension().getHibernateService();
                IJDBCReader reader = service.newJDBCReader(configuration, strategy);
                db = service.newDatabaseCollector(reader);
                reader.readDatabaseSchema(db, new ProgressListener(monitor));
            } catch (UnsupportedOperationException he) {
                throw new HibernateException(he);
            } catch (Exception he) {
                he.printStackTrace();
                throw new HibernateException(he.getMessage(), he.getCause());
            }
            return db;
        }
    });
}
Also used : IProgressListener(org.jboss.tools.hibernate.runtime.spi.IProgressListener) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) IDatabaseCollector(org.jboss.tools.hibernate.runtime.spi.IDatabaseCollector) IService(org.jboss.tools.hibernate.runtime.spi.IService) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) IJDBCReader(org.jboss.tools.hibernate.runtime.spi.IJDBCReader)

Example 2 with HibernateException

use of org.jboss.tools.hibernate.runtime.spi.HibernateException 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 HibernateException(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) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) Ejb3Configuration(org.hibernate.ejb.Ejb3Configuration)

Example 3 with HibernateException

use of org.jboss.tools.hibernate.runtime.spi.HibernateException 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 HibernateException("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) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) 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) DialectResolutionInfo(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)

Example 4 with HibernateException

use of org.jboss.tools.hibernate.runtime.spi.HibernateException in project jbosstools-hibernate by jbosstools.

the class ServiceImpl method newReverseEngineeringStrategy.

@SuppressWarnings("unchecked")
private ReverseEngineeringStrategy newReverseEngineeringStrategy(final String className, ReverseEngineeringStrategy delegate) {
    try {
        Class<ReverseEngineeringStrategy> clazz = (Class<ReverseEngineeringStrategy>) ReflectHelper.classForName(className);
        Constructor<ReverseEngineeringStrategy> constructor = clazz.getConstructor(new Class[] { ReverseEngineeringStrategy.class });
        return constructor.newInstance(new Object[] { delegate });
    } catch (NoSuchMethodException e) {
        try {
            Class<?> clazz = ReflectHelper.classForName(className);
            ReverseEngineeringStrategy rev = (ReverseEngineeringStrategy) clazz.newInstance();
            return rev;
        } catch (Exception eq) {
            throw new HibernateException(eq);
        }
    } catch (Exception e) {
        throw new HibernateException(e);
    }
}
Also used : HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) IReverseEngineeringStrategy(org.jboss.tools.hibernate.runtime.spi.IReverseEngineeringStrategy) DefaultReverseEngineeringStrategy(org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy) ReverseEngineeringStrategy(org.hibernate.cfg.reveng.ReverseEngineeringStrategy) PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) SQLException(java.sql.SQLException)

Example 5 with HibernateException

use of org.jboss.tools.hibernate.runtime.spi.HibernateException 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 HibernateException("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 HibernateException(e);
    }
    return result;
}
Also used : Configuration(org.hibernate.cfg.Configuration) JDBCMetaDataConfiguration(org.hibernate.cfg.JDBCMetaDataConfiguration) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration)

Aggregations

HibernateException (org.jboss.tools.hibernate.runtime.spi.HibernateException)36 SQLException (java.sql.SQLException)18 PersistentClass (org.hibernate.mapping.PersistentClass)12 RootClass (org.hibernate.mapping.RootClass)12 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)12 Dialect (org.hibernate.dialect.Dialect)9 DatabaseMetaDataDialectResolutionInfoAdapter (org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter)9 DialectFactory (org.hibernate.engine.jdbc.dialect.spi.DialectFactory)9 DialectResolutionInfo (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)9 DialectResolutionInfoSource (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource)9 ServiceRegistry (org.hibernate.service.ServiceRegistry)9 MetaDataDialect (org.hibernate.cfg.reveng.dialect.MetaDataDialect)8 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)8 Configuration (org.hibernate.cfg.Configuration)4 JDBCMetaDataConfiguration (org.hibernate.cfg.JDBCMetaDataConfiguration)4 DefaultReverseEngineeringStrategy (org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy)4 ReverseEngineeringStrategy (org.hibernate.cfg.reveng.ReverseEngineeringStrategy)4 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)4 IReverseEngineeringStrategy (org.jboss.tools.hibernate.runtime.spi.IReverseEngineeringStrategy)4 ISchemaExport (org.jboss.tools.hibernate.runtime.spi.ISchemaExport)4