我的插入查询返回所插入的记录的UUID。以下是相关代码。
KeyHolder keyHolder = new GeneratedKeyHolder();
template.update(connection -> {
PreparedStatement ps = connection.prepareStatement(insertQuery);
ps.setString(1, stateName);
ps.setString(2, stateAb);
ps.setObject(3, propertyDetailsObject);
return ps;
}, keyHolder);
我使用的是以select id from tmpproperty limit 1;
结尾的长CTE查询,我在这里缩短了内容,以便更容易的可读性。
但是我得到以下例外
org.springframework.dao.daaintegrityviolationException: 准备的StatementCallback;当没有的时候返回结果 预期的。;嵌套异常是org.postgresql.util.psqlexception:a 当没有预期的时候返回结果。
我在网上找到了上述示例,并期望它能起作用。知道我应该做什么不同吗?
需要更改为:
PreparedStatement ps = connection.prepareStatement(youSQL, new String[]{"id"});
其中 id
是主要键列名
和删除select id from tmpproperty limit 1;
,更新查询就足够了。