【问题标题】:jpql native query not setting parameterjpql本机查询未设置参数
【发布时间】:2014-09-19 18:33:44
【问题描述】:
@Repository
public interface GroupRepository extends JpaRepository<Group, String> {    
//Other queries....

@Query(value = "with cte(group_id, parent_group_id, group_name) as( "
        + "select group_id, parent_group_id, group_name "
            + "from hea.hea_group "
                + "where group_id = ?1 "
        + "union all "
        + "select g.group_id, g.parent_group_id, g.group_name "
            + "from hea.hea_group g "
            + "inner join cte on cte.group_id = g.parent_group_id "
                + "where g.parent_group_id is not null "
        + ") select * from cte", nativeQuery = true)
List<Object> getChildGroups(String groupId);
}

上面是我编写的查询,它应该返回父组及其所有子组。当我用硬编码的组 id 值替换 ?1 并将方法更改为没有参数时,查询会执行它应该做的事情,但是当我尝试像上面那样运行它时,即使我传入它也不会返回任何内容与我硬编码时完全相同的值。

下面是查询生成的sql。当我更换 ?使用组 ID 在测试数据库上运行它会返回它应该返回的结果。

    with cte(group_id, parent_group_id, group_name) as( select
    group_id,
    parent_group_id,
    group_name 
from
    hea.hea_group 
where
    group_id = ? 
union
all select
    g.group_id,
    g.parent_group_id,
    g.group_name 
from
    hea.hea_group g 
inner join
    cte 
        on cte.group_id = g.parent_group_id 
where
    g.parent_group_id is not null ) select
    * 
from
    cte

【问题讨论】:

  • 检查 JPA 发出的 SQL。这通常是通过为您的 JPA 提供程序打开日志记录来完成的。
  • 我在问题里加了SQL,没看出有什么问题,好像知道group_id是个变量。
  • 如何设置参数?请同时添加代码。

标签: spring jpa spring-data jpql spring-data-jpa


【解决方案1】:

变量是从零开始的,所以 ?0 是你应该使用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2013-09-13
    • 2017-08-29
    • 2019-03-15
    • 2012-08-16
    • 1970-01-01
    相关资源
    最近更新 更多