Java Code Examples for org.hibernate.Session.doWork()

The following are Jave code examples for showing how to use doWork() of the org.hibernate.Session class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: postguice   File: Sequence.java   Source Code and License Vote up 9 votes
@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);
				}
			}
		}
	});
}
 
Example 2
Project: query-search   File: BaseJpaTest.java   Source Code and License Vote up 7 votes
@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");
			}
		}
	});
}
 
Example 3
Project: SqlSauce   File: DbUtils.java   Source Code and License Vote up 6 votes
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;
    }