I solved the problem by removing "INNER JOIN" from that SQL2 string.
Two JdbcTemplate update statements in one method
-
21-07-2023 - |
سؤال
I am trying to do two JdbcTemplate updates in same method, but only the first one executes. How can I do two update statements in one method? EduId and timeId returns correct values and I guess the SQL syntax is valid so the problem isn't there.
EDIT: I am using MySQL.
@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.SERIALIZABLE, readOnly=false)
public void moveEdu(int eduId, int timeId) {
final String sql = "UPDATE timeslot ts"
+ " INNER JOIN eduevent ee ON ts.edu_id = ee.edu_id"
+ " SET ts.edu_id = null"
+ " WHERE ts.edu_id = ?;";
Object[] parameters = new Object[] {eduId};
final String sql2 = "UPDATE timeslot ts"
+ " INNER JOIN eduevent ee ON ts.edu_id = ee.edu_id"
+ " SET ts.edu_id = ?"
+ " WHERE ts.time_id = ?;";
Object[] parameters2 = new Object[] {eduId, timeId};
jdbcTemplate.update(sql, parameters);
jdbcTemplate.update(sql2, parameters2);
}
المحلول 2
نصائح أخرى
You code looks fine. You can try to do all your updates in one
...
SET ts.edu_id=CASE
WHEN ts.time_id=? THEN ? <-- time_id=timeID then eduID
ELSE null
END
WHERE ts.time_id = ? OR ts.edu_id = ?
and params should be
Object[] parameters = new Object[] {timeId, eduId, timeId, eduId};
لا تنتمي إلى StackOverflow