无法使用springboot和jdbctemplate在单个事务中插入子表。
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public Response addUser(@RequestBody UserVo userVo) throws Exception
{
service.addUserRecord(userVo);
}
@Autowired
private Dao dao;
@Transactional(readOnly = false)
public void addUserRecord(@RequestBody UserVo userVo) throws Exception
{
int parentSeqNo = dao.addUser();
int result = dao.addUserDetails(parentSeqNo, list);
}
@Autowired
private JdbcTemplate jdbcTemplate;
public int addUser()
{
try
{
String sql = "INSERT INTO user_table() VALUES (?,?,?,?,?) ";
jdbcTemplate.update(sql,....);
return seqNo;
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
public void addUserDetails(String seqNo, List<String> list){
String sql="insert into user_details_table values(?,?)";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
String img = list.get(i);
ps.setString(1, seqNo);
ps.setString(2, img);
}
public int getBatchSize() {
return list.size();
}
});
return;
}
ORA-02291:违反完整性约束(FK3)-找不到父密钥;嵌套的异常是java.sql.SQLIntegrityConstraintViolationException:
数据库表和约束看起来不错,并尝试使用sql编辑器插入,但效果很好。从邮递员工具进行测试时,找不到父键异常。非常感谢您对此问题的任何建议。