jpql native query not setting parameter

时间:2014-09-19 18:33:45

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

@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

1 个答案:

答案 0 :(得分:-1)

变量基于零,所以你应该使用0。