わかりました、これが私の解決策です。それが最高かどうかはわかりませんが、機能します:
これが主な方法です。
public void importTargetValue(NumberImportData data, Target target, ImportType type) throws SQLException {
List<Statement> stmtList = new ArrayList<Statement>();
Connection conn = null;
PreparedStatement stmtTarget = null;
PreparedStatement stmtNumber = null;
PreparedStatement stmtTargetNumber = null;
try {
conn = getConnection();
Long returnedTargetId = insertTarget(conn, stmtTarget, target);
for (TargetNumber t : data.getNumbers()) {
Long returnedNumberId = insertNumber(conn, stmtNumber, t);
//insert to decompozition
insertTargetNumber(conn, stmtTargetNumber, returnedTargetId, returnedNumberId);
}
} finally {
stmtList.add(stmtTarget);
stmtList.add(stmtNumber);
stmtList.add(stmtTargetNumber);
closeDbResources(conn, stmtList);
}
}
Target_of_numbers の挿入:
private Long insertTarget(Connection conn, Statement stmtTarget, Target target) throws SQLException {
PreparedStatement statement = (PreparedStatement) stmtTarget;
ResultSet generatedKeys = null;
try {
statement = conn.prepareStatement("INSERT INTO stats_target (name) VALUES (?)", Statement.RETURN_GENERATED_KEYS);
statement.setString(1, target.getName());
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new SQLException("Creating target failed, no rows affected.");
}
generatedKeys = statement.getGeneratedKeys();
if (generatedKeys.next()) {
target.setTargetId(generatedKeys.getLong(1));
} else {
throw new SQLException("Creating target failed, no generated key obtained.");
}
} finally {
if (generatedKeys != null) try { generatedKeys.close(); } catch (SQLException logOrIgnore) {}
}
return target.getTargetId();
}
数字の挿入:
private Long insertNumber(Connection conn, PreparedStatement stmtNumber, TargetNumber t) throws SQLException {
PreparedStatement statement = (PreparedStatement) stmtNumber;
ResultSet generatedKeys = null;
try {
statement = conn.prepareStatement("REPLACE INTO stats_number (msisdn) VALUES (?)", Statement.RETURN_GENERATED_KEYS);
statement.setString(1, t.getMsisdn());
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new SQLException("Creating number failed, no rows affected.");
}
generatedKeys = statement.getGeneratedKeys();
if (generatedKeys.next()) {
t.setNumberId(generatedKeys.getLong(1));
} else {
throw new SQLException("Creating number failed, no generated key obtained.");
}
} finally {
if (generatedKeys != null) try { generatedKeys.close(); } catch (SQLException logOrIgnore) {}
}
return t.getNumberId();
}
分解に挿入:
private void insertTargetNumber(Connection conn, PreparedStatement stmtTargetNumber, Long returnedTargetId, Long returnedNumberId) throws SQLException {
PreparedStatement statement = (PreparedStatement) stmtTargetNumber;
try {
statement = conn.prepareStatement("INSERT INTO stats_target_number (targetId, numberId) VALUES (?, ?)");
statement.setLong(1, returnedTargetId);
statement.setLong(2, returnedNumberId);
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new SQLException("Creating target failed, no rows affected.");
}
} finally {
}
}
そしてステートメントを閉じるいくつかの方法:
protected final void closeDbResources(Connection conn, List<Statement> stmt) {
for (Statement s : stmt) {
if (s != null) {
log.debug("[SQL QUERY]: " + s);
try {
s.close();
} catch (SQLException e) {}
}
}
}