org.hibernate.jdbc.ReturningWork Java Examples

The following examples show how to use org.hibernate.jdbc.ReturningWork. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CachedStatement.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the DB connection from Hibernate and run some work on it. Since we
 * will use it to run queries/stored procs, this will also flush the session
 * to ensure that stored procs will see changes made in the Hibernate cache
 */
private <T> T doWithStolenConnection(ReturningWork<T> work) throws HibernateException {
    Session session = HibernateFactory.getSession();
    if (session.getFlushMode().equals(FlushModeType.AUTO)) {
        session.flush();
    }
    return session.doReturningWork(work);
}
 
Example #2
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T doReturningWork(final ReturningWork<T> work) throws HibernateException {
	WorkExecutorVisitable<T> realWork = new WorkExecutorVisitable<T>() {
		@Override
		public T accept(WorkExecutor<T> workExecutor, Connection connection) throws SQLException {
			return workExecutor.executeReturningWork( work, connection );
		}
	};
	return doWork( realWork );
}
 
Example #3
Source File: DefaultSequenceHandlerRepository.java    From pnc with Apache License 2.0 5 votes vote down vote up
@Override
public Long getNextID(final String sequenceName) {

    ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() {
        @Override
        public Long execute(Connection connection) throws SQLException {
            DialectResolver dialectResolver = new StandardDialectResolver();
            Dialect dialect = dialectResolver.resolveDialect(getResolutionInfo(connection));
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement(dialect.getSequenceNextValString(sequenceName));
                resultSet = preparedStatement.executeQuery();
                resultSet.next();
                return resultSet.getLong(1);
            } catch (SQLException e) {
                throw e;
            } finally {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (resultSet != null) {
                    resultSet.close();
                }
            }

        }
    };

    Session session = (Session) entityManager.getDelegate();
    SessionFactory sessionFactory = session.getSessionFactory();

    Long maxRecord = sessionFactory.getCurrentSession().doReturningWork(maxReturningWork);
    return maxRecord;
}
 
Example #4
Source File: DefaultSequenceHandlerRepository.java    From pnc with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sequenceExists(final String sequenceName) {
    ReturningWork<Boolean> work = new ReturningWork<Boolean>() {
        @Override
        public Boolean execute(Connection connection) throws SQLException {
            DialectResolver dialectResolver = new StandardDialectResolver();
            Dialect dialect = dialectResolver.resolveDialect(getResolutionInfo(connection));
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement(dialect.getQuerySequencesString());
                resultSet = preparedStatement.executeQuery();
                while (resultSet.next()) {
                    if (sequenceName.equals(resultSet.getString(1))) {
                        return true;
                    }
                }
            } catch (SQLException e) {
                throw e;
            } finally {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (resultSet != null) {
                    resultSet.close();
                }
            }
            return false;

        }
    };

    Session session = (Session) entityManager.getDelegate();
    SessionFactory sessionFactory = session.getSessionFactory();
    return sessionFactory.getCurrentSession().doReturningWork(work);
}
 
Example #5
Source File: SessionDelegatorBaseImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public <T> T doReturningWork(ReturningWork<T> work) throws HibernateException {
	return delegate.doReturningWork( work );
}
 
Example #6
Source File: Session.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Controller for allowing users to perform JDBC related work using the Connection managed by this Session.  After
 * execution returns the result of the {@link ReturningWork#execute} call.
 *
 * @param work The work to be performed.
 * @param <T> The type of the result returned from the work
 *
 * @return the result from calling {@link ReturningWork#execute}.
 *
 * @throws HibernateException Generally indicates wrapped {@link java.sql.SQLException}
 */
<T> T doReturningWork(ReturningWork<T> work) throws HibernateException;