Question

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);     
}
Was it helpful?

Solution 2

I solved the problem by removing "INNER JOIN" from that SQL2 string.

OTHER TIPS

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};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top