Example usage for org.hibernate.internal SessionFactoryImpl getSettings

List of usage examples for org.hibernate.internal SessionFactoryImpl getSettings

Introduction

In this page you can find the example usage for org.hibernate.internal SessionFactoryImpl getSettings.

Prototype

@SuppressWarnings("deprecation")
    public Settings getSettings() 

Source Link

Usage

From source file:com.evolveum.midpoint.repo.sql.helpers.OrgClosureManager.java

License:Apache License

private boolean autoUpdateClosureTableStructure() {

    if (baseHelper.getConfiguration().isSkipOrgClosureStructureCheck()) {
        LOGGER.debug("Skipping org closure structure check.");
        return false;
    }/*from ww w .  ja  v a 2  s  .  c om*/

    SessionFactory sf = baseHelper.getSessionFactory();
    if (sf instanceof SessionFactoryImpl) {
        SessionFactoryImpl sfi = ((SessionFactoryImpl) sf);
        LOGGER.debug("SessionFactoryImpl.getSettings() = {}; auto update schema = {}", sfi.getSettings(),
                sfi.getSettings() != null ? sfi.getSettings().isAutoUpdateSchema() : null);
        if (sfi.getSettings() != null && sfi.getSettings().isAutoUpdateSchema()) {

            LOGGER.info("Checking the closure table structure.");

            final Session session = baseHelper.getSessionFactory().openSession();
            final Holder<Boolean> wrongNumberOfColumns = new Holder<>(false);
            session.doWork(new Work() {
                @Override
                public void execute(Connection connection) throws SQLException {
                    DatabaseMetaData meta = connection.getMetaData();
                    if (meta == null) {
                        LOGGER.warn("No database metadata found.");
                    } else {
                        ResultSet rsColumns = meta.getColumns(null, null, CLOSURE_TABLE_NAME, null);
                        int columns = 0;
                        while (rsColumns.next()) {
                            LOGGER.debug("Column: {} {}", rsColumns.getString("TYPE_NAME"),
                                    rsColumns.getString("COLUMN_NAME"));
                            columns++;
                        }
                        if (columns > 0) {
                            LOGGER.debug("There are {} columns in {} (obtained via DatabaseMetaData)", columns,
                                    CLOSURE_TABLE_NAME);
                            if (columns != 3) {
                                wrongNumberOfColumns.setValue(true);
                            }
                            return;
                        }
                        // perhaps some problem here... let's try another way out
                        try {
                            Statement stmt = connection.createStatement();
                            ResultSet rs = stmt.executeQuery("select * from " + CLOSURE_TABLE_NAME);
                            int cols = rs.getMetaData().getColumnCount();
                            if (cols > 0) {
                                LOGGER.debug(
                                        "There are {} columns in {} (obtained via resultSet.getMetaData())",
                                        cols, CLOSURE_TABLE_NAME);
                                if (cols != 3) {
                                    wrongNumberOfColumns.setValue(true);
                                }
                            } else {
                                LOGGER.warn(
                                        "Couldn't determine the number of columns in {}. In case of problems, please fix your database structure manually using DB scripts in 'config' folder.",
                                        CLOSURE_TABLE_NAME);
                            }
                            rs.close(); // don't care about closing them in case of failure
                            stmt.close();
                        } catch (RuntimeException e) {
                            LoggingUtils.logException(LOGGER,
                                    "Couldn't obtain the number of columns in {}. In case of problems running midPoint, please fix your database structure manually using DB scripts in 'config' folder.",
                                    e, CLOSURE_TABLE_NAME);
                        }
                    }
                }
            });
            if (wrongNumberOfColumns.getValue()) {
                session.getTransaction().begin();
                LOGGER.info("Wrong number of columns detected; dropping table " + CLOSURE_TABLE_NAME);
                Query q = session.createSQLQuery("drop table " + CLOSURE_TABLE_NAME);
                q.executeUpdate();
                session.getTransaction().commit();

                LOGGER.info(
                        "Calling hibernate hbm2ddl SchemaUpdate tool to create the table in the necessary form.");
                new SchemaUpdate(sfi.getServiceRegistry(),
                        baseHelper.getSessionFactoryBean().getConfiguration()).execute(false, true);
                LOGGER.info(
                        "Done, table was (hopefully) created. If not, please fix your database structure manually using DB scripts in 'config' folder.");
                return true;
            }
        } else {
            // auto schema update is disabled
        }
    } else {
        LOGGER.warn("SessionFactory is not of type SessionFactoryImpl; it is {}",
                sf != null ? sf.getClass() : "null");
    }
    return false;
}

From source file:fr.univrouen.poste.web.admin.IndexController.java

License:Apache License

private String getHbm2ddlAuto() {
    Session session = entityManager.unwrap(Session.class);
    SessionFactoryImpl sessionImpl = (SessionFactoryImpl) session.getSessionFactory();
    Settings setting = sessionImpl.getSettings();
    String hbm2ddlAuto = setting.isAutoCreateSchema() ? "create" : "update";
    return hbm2ddlAuto;
}

From source file:org.babyfish.hibernate.cfg.Configuration.java

License:Open Source License

@SuppressWarnings("unchecked")
static void checkNamedQueries(SessionFactoryImpl factory) {
    Settings settings = factory.getSettings();
    if (settings.isNamedQueryStartupCheckingEnabled()) {
        Map<String, HibernateException> errors;
        try {/*  www .ja  v  a 2  s .  c  om*/
            errors = (Map<String, HibernateException>) CHECK_NAMED_QUERIES_METHOD.invoke(factory);
        } catch (IllegalAccessException ex) {
            throw new AssertionError();
        } catch (InvocationTargetException ex) {
            throw UncheckedException.rethrow(ex.getTargetException());
        }
        if (!errors.isEmpty()) {
            boolean addComma = false;
            StringBuilder builder = new StringBuilder("Errors in named queries: ");
            for (Entry<String, HibernateException> entry : errors.entrySet()) {
                String queryName = entry.getKey();
                HibernateException e = entry.getValue();
                if (addComma) {
                    builder.append(", ");
                } else {
                    addComma = true;
                }
                builder.append(queryName);
                sessionFactoryImplLog.error("Error in named query: " + queryName, e);
            }
            throw new HibernateException(builder.toString());
        }
    }
}