public <T> List<T> executeWithKeys(Class<T> type) { return executeWithKeys(type, null); }
/** * Execute the clause and return the generated key with the type of the given path. * If no rows were created, or the referenced column is not a generated key, null is returned. * Otherwise, the key of the first row is returned. * * @param <T> * @param path path for key * @return generated keys */ @SuppressWarnings("unchecked") public <T> List<T> executeWithKeys(Path<T> path) { return executeWithKeys((Class<T>) path.getType(), path); }
protected <T> T executeWithKey(Class<T> type, @Nullable Path<T> path) { ResultSet rs = executeWithKeys(); try { if (rs.next()) { return configuration.get(rs, path, 1, type); } else { return null; } } catch (SQLException e) { throw configuration.translate(e); } finally { close(rs); } }
protected <T> List<T> executeWithKeys(Class<T> type, @Nullable Path<T> path) { ResultSet rs = null; try { rs = executeWithKeys(); List<T> rv = new ArrayList<T>(); while (rs.next()) { rv.add(configuration.get(rs, path, 1, type)); } return rv; } catch (SQLException e) { throw configuration.translate(e); } finally { if (rs != null) { close(rs); } reset(); } }
@Test @ExcludeIn({CUBRID, DB2, DERBY, POSTGRESQL, SQLSERVER, TERADATA}) public void merge_with_keys_Null_Id() throws SQLException { ResultSet rs = merge(survey).keys(survey.id) .setNull(survey.id) .set(survey.name, "Hello World").executeWithKeys(); assertTrue(rs.next()); assertTrue(rs.getObject(1) != null); rs.close(); }
@Test @ExcludeIn({H2, CUBRID, SQLSERVER}) public void merge_with_keys() throws SQLException { ResultSet rs = merge(survey).keys(survey.id) .set(survey.id, 7) .set(survey.name, "Hello World").executeWithKeys(); assertTrue(rs.next()); assertTrue(rs.getObject(1) != null); rs.close(); }
@Test @ExcludeIn({H2, CUBRID, SQLSERVER}) public void merge_with_keys_listener() throws SQLException { final AtomicBoolean result = new AtomicBoolean(); SQLListener listener = new SQLBaseListener() { @Override public void end(SQLListenerContext context) { result.set(true); } }; SQLMergeClause clause = merge(survey).keys(survey.id) .set(survey.id, 7) .set(survey.name, "Hello World"); clause.addListener(listener); ResultSet rs = clause.executeWithKeys(); assertTrue(rs.next()); assertTrue(rs.getObject(1) != null); rs.close(); assertTrue(result.get()); }