本文整理汇总了Java中org.hibernate.Session.doWork方法的典型用法代码示例。如果您正苦于以下问题:Java Session.doWork方法的具体用法?Java Session.doWork怎么用?Java Session.doWork使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.hibernate.Session
的用法示例。
在下文中一共展示了Session.doWork方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addBatch
import org.hibernate.Session; //导入方法依赖的package包/类
@SneakyThrows
private void addBatch() {
final String query = "select nextval('" + name + "') from generate_series(1, " + batchSize + ")";
final Session session = EM.em().unwrap(Session.class);
session.doWork(connection -> {
try (final PreparedStatement stmt = connection.prepareStatement(query)) {
try (final ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
final long value = rs.getLong(1);
pending.add(value);
}
}
}
});
}
示例2: initializeDatabase
import org.hibernate.Session; //导入方法依赖的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");
}
}
});
}
示例3: getColumnNameToIndexMap
import org.hibernate.Session; //导入方法依赖的package包/类
public static Map<String, Integer> getColumnNameToIndexMap(final String queryString, final EntityManager em) throws SQLException {
final Session session = em.unwrap(Session.class); // ATTENTION! This is Hibernate-specific!
final AtomicReference<ResultSetMetaData> msRef = new AtomicReference<>();
session.doWork((c) -> {
try (final PreparedStatement statement = create(c, queryString)) {
// I'm not setting parameters here, because I just want to find out about the return values' column names
msRef.set(statement.getMetaData());
}
});
final ResultSetMetaData metaData = msRef.get();
// LinkedHashmap preserves order of insertion:
final Map<String, Integer> columnNameToColumnIndex = new LinkedHashMap<>();
for (int t = 0; t < metaData.getColumnCount(); ++t) {
// important, first index in the metadata is "1", the first index for the result array must be "0"
columnNameToColumnIndex.put(metaData.getColumnName(t + 1), t);
}
return columnNameToColumnIndex;
}