下面列出了怎么用org.hibernate.jdbc.ReturningWork的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
@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 );
}
@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;
}
@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);
}
@Override
public <T> T doReturningWork(ReturningWork<T> work) throws HibernateException {
return delegate.doReturningWork( work );
}
/**
* 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;