本文整理汇总了Java中org.hibernate.jdbc.Work类的典型用法代码示例。如果您正苦于以下问题:Java Work类的具体用法?Java Work怎么用?Java Work使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Work类属于org.hibernate.jdbc包,在下文中一共展示了Work类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initializeDatabase
import org.hibernate.jdbc.Work; //导入依赖的package包/类
@Before
public void initializeDatabase() {
Session session = entityManager.unwrap(Session.class);
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
try {
File script = new File(getClass().getResource("/data.sql").getFile());
RunScript.execute(connection, new FileReader(script));
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("Database initialize script error");
}
}
});
}
示例2: executeBatch
import org.hibernate.jdbc.Work; //导入依赖的package包/类
@Transactional(readOnly = false)
public void executeBatch(final String sql, final Object[]... parameters) {
getSession().doWork(new Work() {
public void execute(Connection connection) throws SQLException {
connection.setAutoCommit(false);
PreparedStatement stmt = connection.prepareStatement(sql);
for (Object[] arr : parameters) {
int i = 1;
for (Object p : arr) {
stmt.setObject(i++, p);
}
stmt.addBatch();
}
stmt.executeBatch();
connection.commit();
}
});
}
示例3: tableExists
import org.hibernate.jdbc.Work; //导入依赖的package包/类
public boolean tableExists(Session session, final String table)
{
final ExtendedDialect locDialect = (ExtendedDialect) this.dialect;
final Boolean[] hasTable = new Boolean[1];
session.doWork(new Work()
{
@Override
public void execute(Connection connection) throws SQLException
{
ResultSet tables = connection.getMetaData().getTables(null, defaultSchema,
locDialect.getNameForMetadataQuery(table, false), new String[]{"TABLE"});
try
{
hasTable[0] = tables.next();
}
finally
{
tables.close();
}
}
});
return hasTable[0];
}
示例4: initializeOrUpgradeSchema
import org.hibernate.jdbc.Work; //导入依赖的package包/类
/**
* Initializes, or upgrades the current QuartzDesk database schema by
* executing the specified list of SQL scripts.
*
* @param scriptUrls the list of SQL scripts to execute.
* @param schemaVersion the version of the schema after the specified SQL scripts have been applied.
*/
public void initializeOrUpgradeSchema( final List<URL> scriptUrls, final Version schemaVersion )
{
Session session = getSessionFactory().getCurrentSession();
session.doWork( new Work()
{
@Override
public void execute( Connection connection )
throws SQLException
{
DatabaseScriptExecutor scriptExecutor = new DatabaseScriptExecutor();
scriptExecutor.addScriptUrls( scriptUrls );
scriptExecutor.executeScripts( connection );
SchemaUpdate schemaUpdate = new SchemaUpdate()
.withMajor( schemaVersion.getMajor() )
.withMinor( schemaVersion.getMinor() )
.withMaintenance( schemaVersion.getMaintenance() )
.withAppliedAt( Calendar.getInstance() );
insertSchemaUpdate( schemaUpdate );
}
} );
}
示例5: listTables
import org.hibernate.jdbc.Work; //导入依赖的package包/类
/**
* List tables from database
*/
private List<String> listTables(Session session) {
final List<String> tables = new ArrayList<String>();
final String[] tableTypes = {"TABLE"};
final String[] tablePatterns = new String[]{"JBPM_%", "OKM_%", "DEFAULT_%", "VERSION_%", "jbpm_%", "okm_%", "default_%",
"version_%"};
session.doWork(new Work() {
@Override
public void execute(Connection con) throws SQLException {
DatabaseMetaData md = con.getMetaData();
for (String table : tablePatterns) {
ResultSet rs = md.getTables(null, null, table, tableTypes);
while (rs.next()) {
tables.add(rs.getString(3));
}
rs.close();
}
}
});
return tables;
}
示例6: insertSqlFile
import org.hibernate.jdbc.Work; //导入依赖的package包/类
/**
* Executes the passed sql files on the EntityManager.
*
* @param entityManager
* the EntityManager
* @param sqlScripts
* sql scripts to execute
*/
public static void insertSqlFile(EntityManager entityManager, final File... sqlScripts) {
entityManager.unwrap(Session.class).doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
// Setup database
try {
for (File file : sqlScripts) {
LOGGER.debug("INSERTing {}", file.getAbsolutePath());
Assume.assumeTrue("SQL-Script not found", file.isFile());
String sql = Resources.toString(file.toURI().toURL(), Charsets.UTF_8);
executeSqlScript(connection, file.getName(), sql);
LOGGER.debug("INSERTing {} ... done", file.getAbsolutePath());
}
} catch (IOException | ScriptException e) {
throw new SQLException(e);
}
}
});
}
示例7: insertSqlString
import org.hibernate.jdbc.Work; //导入依赖的package包/类
/**
* Executes the passed sql queries on the EntityManager.
*
* @param entityManager
* the EntityManager
*
* @param sqlQuery
* queries to execute
*/
public static void insertSqlString(EntityManager entityManager, final String... sqlQuery) {
entityManager.unwrap(Session.class).doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
// Setup database
try {
for (String query : sqlQuery) {
LOGGER.debug("INSERTing '{}'", query);
executeSqlScript(connection, null, query);
LOGGER.debug("INSERTing '{}' ... done", query);
}
} catch (ScriptException e) {
throw new SQLException(e);
}
}
});
}
示例8: insertDbUnitTestdata
import org.hibernate.jdbc.Work; //导入依赖的package包/类
/**
* Inserts test data in DbUnit XML format.
*
* @param entityManager
* the EntityManager
* @param dbUnitTestdata
* test file stream
*/
public static void insertDbUnitTestdata(EntityManager entityManager, final InputStream dbUnitTestdata) {
entityManager.unwrap(Session.class).doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
// Insert Testdata
try {
LOGGER.debug("INSERTing testdata");
DatabaseConnection databaseConnection = new DatabaseConnection(connection);
databaseConnection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
new MySqlDataTypeFactory());
FlatXmlDataSet dataSet = new FlatXmlDataSet(
new FlatXmlProducer(new InputSource(dbUnitTestdata), false, true));
DatabaseOperation.CLEAN_INSERT.execute(databaseConnection, dataSet);
LOGGER.debug("INSERTing testdata ... done");
} catch (DatabaseUnitException e) {
throw new SQLException(e);
}
}
});
}
示例9: beginTransaction
import org.hibernate.jdbc.Work; //导入依赖的package包/类
@Override
public Object beginTransaction(final EntityManager entityManager,
final TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException {
Session session = (Session) entityManager.getDelegate();
if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
getSession(entityManager).getTransaction().setTimeout(definition.getTimeout());
}
entityManager.getTransaction().begin();
logger.debug("Transaction started");
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
logger.debug("The connection instance is " + connection.toString());
logger.debug("The isolation level of the connection is " + connection.getTransactionIsolation()
+ " and the isolation level set on the transaction is " + definition.getIsolationLevel() );
DataSourceUtils.prepareConnectionForTransaction(connection, definition);
}
});
return prepareTransaction(entityManager, definition.isReadOnly(), definition.getName());
}
示例10: testTablesAndSequences
import org.hibernate.jdbc.Work; //导入依赖的package包/类
/**
* <p>Check table and sequence naming. Important because sequence handling is customized through
* {@link PerTableSequenceStyleGenerator} and {@link PerTableSequenceStrategyProvider}.</p>
*
* <p>We check that {@link Person} entity creates a {@code person} table and a {@code person_id_seq} sequence.</p>
*/
@Test
public void testTablesAndSequences() {
EntityManager entityManager = entityManagerUtils.getEntityManager();
((Session) entityManager.getDelegate()).doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
String expectedTableName = Person.class.getSimpleName().toLowerCase(); // person
String expectedSequenceName = expectedTableName + "_id_seq"; // person_id_seq
JdbcRelation table = getRelation(connection, configurationProvider.getDefaultSchema(),
expectedTableName, JdbcDatabaseMetaDataConstants.REL_TYPE_TABLE);
Assert.assertEquals(expectedTableName, table.getTable_name());
JdbcRelation sequence = getRelation(connection, configurationProvider.getDefaultSchema(),
expectedSequenceName, JdbcDatabaseMetaDataConstants.REL_TYPE_SEQUENCE);
Assert.assertEquals(expectedSequenceName, sequence.getTable_name());
}
});
}
示例11: openSession
import org.hibernate.jdbc.Work; //导入依赖的package包/类
@Test
public void openSession() throws Exception {
createSessionFactory();
//
Session session = sessionFactory.openSession();
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
System.out.println("connection: " + connection);
System.out.println("autoCommit: " + connection.getAutoCommit());
System.out.println("transactionIsolation: "
+ connection.getTransactionIsolation());
}
});
System.out.println("flushMode: " + session.getFlushMode());//FlushMode.AUTO
System.out.println(session);
assertNotNull(session);
}
示例12: openSession
import org.hibernate.jdbc.Work; //导入依赖的package包/类
@Test
public void openSession() throws Exception {
createSessionFactory();
//
Session session = sessionFactory.openSession();
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
System.out.println("connection: " + connection);
System.out.println("autoCommit: " + connection.getAutoCommit());
System.out.println("transactionIsolation: " + connection.getTransactionIsolation());
}
});
System.out.println("flushMode: " + session.getFlushMode());
System.out.println(session);
assertNotNull(session);
}
示例13: openSession
import org.hibernate.jdbc.Work; //导入依赖的package包/类
@Test
public void openSession() throws Exception {
Configuration config = new Configuration().configure("hibernate.cfg.xml");
// SessionFactory sessionFactory = config.buildSessionFactory();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
.buildServiceRegistry();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
System.out.println("sessionFactory: " + sessionFactory);
Session session = sessionFactory.openSession();
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
System.out.println("connection: " + connection);
System.out.println("getAutoCommit: " + connection.getAutoCommit());
System.out.println("getTransactionIsolation: " + connection.getTransactionIsolation());
}
});
}
示例14: beginTransaction
import org.hibernate.jdbc.Work; //导入依赖的package包/类
@Override
public Object beginTransaction(EntityManager entityManager,
final TransactionDefinition definition) throws PersistenceException,
SQLException, TransactionException {
Session session = entityManager.unwrap(Session.class);
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
DataSourceUtils.prepareConnectionForTransaction(connection, definition);
if (connection.isReadOnly() && !definition.isReadOnly()) {
connection.setReadOnly(false);
}
}
});
entityManager.getTransaction().begin();
return prepareTransaction(entityManager, definition.isReadOnly(), definition.getName());
}
示例15: runScript
import org.hibernate.jdbc.Work; //导入依赖的package包/类
public void runScript() {
getSession().doWork(new Work() {
public void execute(Connection conn) throws SQLException {
if(JdbcHelper.isExec(conn)) {
return;
}
try {
String databaseType = JdbcHelper.getDatabaseType(conn);
String schema = "db/core/schema-" + databaseType + ".sql";
ScriptRunner runner = new ScriptRunner(conn, true);
runner.runScript(schema);
} catch (Exception e) {
throw new SnakerException(e);
}
}
});
}