Example usage for org.hibernate Session doWork

List of usage examples for org.hibernate Session doWork

Introduction

In this page you can find the example usage for org.hibernate Session doWork.

Prototype

void doWork(Work work) throws HibernateException;

Source Link

Document

Controller for allowing users to perform JDBC related work using the Connection managed by this Session.

Usage

From source file:test.googlecode.genericdao.jpa.JPAHibernatePersistenceHelper.java

License:Apache License

public void executeWithJdbcConnection(final ExecutableWithJdbcConnection executable) {
    Session hibernateSession = (Session) entityManager.getDelegate();
    hibernateSession.doWork(new Work() {
        public void execute(Connection connection) throws SQLException {
            executable.execute(connection);
        }//from  w w  w.j  a v a  2s.  c  o m
    });
}

From source file:utilities.internal.CopyOfDatabaseUtil.java

License:Open Source License

private void executeScript(final List<String> script) {
    Session session;
    session = entityManager.unwrap(Session.class);
    session.doWork(new Work() {
        @Override//  w  ww . ja v  a2s. co m
        public void execute(Connection connection) throws SQLException {
            Statement statement;

            statement = connection.createStatement();
            for (String line : script) {
                statement.execute(line);
            }
            connection.commit();
        }
    });
}

From source file:utils.DatabaseResetUtils.java

License:Apache License

public static void resetDatabase(DatabaseManager databaseManager, String filename) {
    lastResetDate = new java.util.Date(System.currentTimeMillis());

    Object value = databaseManager.getCourseGroupAppointmentManager();
    Field field = null;/*from   w ww.  j ava2s  . co  m*/
    try {
        field = value.getClass().getDeclaredField("databaseManager");
        field.setAccessible(true);
        value = field.get(value);

        field = value.getClass().getDeclaredField("testDatabaseManager");
        field.setAccessible(true);
        value = field.get(value);

        field = value.getClass().getDeclaredField("sessionFactory");
        field.setAccessible(true);
        value = field.get(value);

        SessionFactory sessionFactory = (SessionFactory) value;
        Session session = sessionFactory.openSession();
        Transaction transaction = null;

        try {
            transaction = session.beginTransaction();
            session.doWork(new Work() {
                @Override
                public void execute(Connection connection) throws SQLException {
                    DatabaseResetUtils databaseResetUtils = new DatabaseResetUtils(connection, false, false);
                    databaseResetUtils.setLogWriter(null);
                    try {
                        Reader reader = new FileReader(filename);
                        databaseResetUtils.runScript(reader);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction != null)
                transaction.rollback();
            throw e;
        } finally {
            if (session != null)
                session.close();
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}