简体   繁体   English

如何将 postgresSql 查询重写为 Spring-data jpa 查询

[英]How to rewrite postgresSql query as Spring-data jpa query

I am stuck at the moment while trying to write the next query as a spring-data JPA query:我在尝试将下一个查询编写为 spring-data JPA 查询时卡住了:

with recursive s as (
select *
from t
where file_id = '12345'
union all
select dfs.*
from t dfs
join s on s.file_id = dfs.parent_folder_id
)
select * from s;

I have tried the next:我已经尝试了下一个:

@Query(value = "with recursive subfiles as (
select * from t where file_id=?1 
union all 
dfs.* from t dfs join subfiles s 
on s.file_id = dfs.parent_folder_id) 
select file_id from subfiles", nativeQuery = true)

But I get the next error:但我收到下一个错误:

Method threw 'org.springframework.dao.InvalidDataAccessResourceUsageException' exception.
could not extract ResultSet; SQL [n/a]
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.postgresql.util.PSQLException: ERROR: syntax error at or near "dfs"

The query should list all direct or indirect dependent children for a specific id.查询应列出特定 ID 的所有直接或间接依赖子项。 (a similar post here ) 这里有一个类似的帖子)

I have managed to fix it using the next format:我已经设法使用下一种格式修复它:

@Query(nativeQuery = true,
value = "with recursive subfiles as " +
  "(select * " +
  "from t " +
  "where file_id=?1 " +
  "union all " +
  "select dfs.* " +
  "from t dfs " +
  "join subfiles s " +
  "on s.file_id = dfs.parent_folder_id) " +
"select file_id from subfiles")
List<String> listAllByParentId(String folderId);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM