After reading this, having experienced a brief moment of failure, looking for some way to get this work to work with the MERGE operator, and then finally accepting the APC comment, I settled on the following alternative, which may be useful for someone else:
public List upsert(String updateSql, String insertSql, Map data){ List ids = new ArrayList(); Map params = new HashMap(); params.put("name", data.get("name")); if(data.get("personId") != null){ params.put("personId", data.get("personId")); int rowCount = jdbcTemplate.update(updateSql, params); if(rowCount == 1){ ids.add(data.get("personId")); } }else{ keyHolder = new GeneratedKeyHolder(); int rowCount = jdbcTemplate.update(insertSql, params, keyHolder); if(rowCount == 1){ ids.add(keyHolder.getKey().intValue()); } } return ids; }
source share