前一次,我的查询对于单行更新工作正常。现在,我必须修改此查询以更新多行。该查询是本地查询,并使用postgresql和postgis。
旧查询:
@Modifying
@Transactional
@Query(value = "WITH tmp AS (SELECT ST_Difference( (SELECT ST_Buffer(ST_Union(ST_Buffer(a.area\\:\\:geometry, 0.002)), -0.002) \n" +
" FROM mydb.city_area a, mydb.dis_city d, mydb.city c \n" +
" where c.id_city=d.id_city and d.id_dis=?1 \n" +
" and c.cod_city=a.cod_city), \n" +
" (ST_Difference( ST_GeomFromGeoJSON(?2)\\:\\:geometry, (SELECT ST_Union(a.area\\:\\:geometry) \n" +
" FROM mydb.city_area a, mydb.dis_city d, mydb.city c \n" +
" where c.id_city=d.id_city and d.id_dis=?1 \n" +
" and c.cod_city=a.cod_city and c.full_area=true) \n" +
" )) \n" +
" ) AS final_area)\n" +
"UPDATE mydb.dis_area SET new_area=(SELECT final_area FROM tmp), " +
"id_type=3 " +
"WHERE id_dis=?1 ",
nativeQuery = true)
Integer insertShape(Integer id, String shapeGeoJson);
在新查询中,我按照here的规定在@Modifying
中添加了一些参数:
@Modifying(flushAutomatically = true, clearAutomatically = true)
@Transactional
@Query(value = "WITH tmp AS (SELECT ST_Difference( (SELECT ST_Buffer(ST_Union(ST_Buffer(a.area\\:\\:geometry, 0.002)), -0.002) \n" +
" FROM mydb.city_area a, mydb.dis_city d, mydb.city c \n" +
" where c.id_city=d.id_city and d.id_dis=?1 \n" +
" and c.cod_city=a.cod_city), \n" +
" (ST_Difference( ST_GeomFromGeoJSON(?2)\\:\\:geometry, (SELECT ST_Union(a.area\\:\\:geometry) \n" +
" FROM mydb.city_area a, mydb.dis_city d, mydb.city c \n" +
" where c.id_city=d.id_city and d.id_dis=?1 \n" +
" and c.cod_city=a.cod_city and c.full_area=true) \n" +
" )) \n" +
" ) AS final_area)\n" +
"UPDATE mydb.dis_area SET new_area=(SELECT final_area FROM tmp), " +
"id_type=3 " +
"WHERE id_dis_aree=(select id_dis_aree from dis_area where id_dis=?1) ",
nativeQuery = true)
Integer insertShape(Integer id, String shapeGeoJson);
但是可悲的是,此更改没有任何效果。 (如果我为来自postgresql的查询提供午餐,它将运行得很好)。 我该怎么解决?
编辑: 我添加了查询,但是它适用于postgresql。唯一的区别是,旧版本:WHERE id_dis=?1
面向单行,而新版本WHERE id_dis_aree=(select id_dis_aree from dis_area where id_dis=?1)
面向多行。
一对id_dis_aree
和id_dis
是主键。两个或多个记录可以具有相同的id_dis_aree
和不同的id_dis
。因此,在第二个查询中,我从id_dis_aree
获取了id_dis
,以影响更多行。
Edit2: 我做了2个测试:
使用固定的有线ID值直接替换最后一个子选择:WHERE id_dis_aree=123456
这样可以工作。这可能是一种解决方法解决方法,可以在调用查询后获取id_dis_aree
。
用以下内容替换最后一个子选择:WHERE id_dis_aree IN (select id_dis_aree from dis_area where id_dis=?1)
不起作用。 (备注:子选择始终返回单个值。)
答案 0 :(得分:0)
我没有找到真正的解决方案,而只是一种解决方法:
我通过调用(select id_dis_aree from dis_area where id_dis=?1)
从@Service
的子查询disAreeRepository.findByIdIdDis(idDis).getId().getIdDisAree()
中获取了值
@Transactional
public Integer insertDis(Integer idDis, String shapeGeoJson) {
return disAreeRepository.
insertShape(
idDis,
shapeGeoJson,
disAreeRepository.findByIdIdDis(idDis).getId().getIdDisAree()
);
}
然后将其作为第三个参数传递给@Repository
本机查询:
@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query(value = "WITH tmp AS (SELECT ST_Difference( (SELECT ST_Buffer(ST_Union(ST_Buffer(a.area\\:\\:geometry, 0.002)), -0.002) \n" +
" FROM mydb.city_area a, mydb.dis_city d, mydb.city c \n" +
" where c.id_city=d.id_city and d.id_dis=?1 \n" +
" and c.cod_city=a.cod_city), \n" +
" (ST_Difference( ST_GeomFromGeoJSON(?2)\\:\\:geometry, (SELECT ST_Union(a.area\\:\\:geometry) \n" +
" FROM mydb.city_area a, mydb.dis_city d, mydb.city c \n" +
" where c.id_city=d.id_city and d.id_dis=?1 \n" +
" and c.cod_city=a.cod_city and c.full_area=true) \n" +
" )) \n" +
" ) AS final_area)\n" +
"UPDATE mydb.dis_area SET new_area=(SELECT final_area FROM tmp), " +
"id_type=3 " +
"WHERE id_dis_aree=?3 ",
nativeQuery = true)
Integer insertShape(Integer id, String shapeGeoJson, Integer idDisAree);