Spring jdbc不能与h2查询递归一起使用

时间:2016-03-15 10:57:27

标签: java spring recursion jdbc h2

有标准表组和ACL。我与组表进行了多次连接,以便能够在组中包含组。现在当我尝试Spring安全性来替换使用H2数据库和请求类型的查询时

@Bean
public JdbcUserDetailsManager userDetailsManager() {
    JdbcUserDetailsManager manager = new JdbcUserDetailsManager();
    manager.setDataSource(dataSource);
    manager.setRolePrefix("GROUP_");
    manager.setEnableGroups(true);
    manager.setEnableAuthorities(false);
    manager.setGroupAuthoritiesByUsernameQuery(
            //h2
            "WITH recursive rec_groups (parent, child, name, authority) AS " +
                    "(SELECT gm.group_id, cast(NULL as bigint), g.group_name, ga.authority " +
                    "FROM wsseddb.group_members as gm, wsseddb.groups g, wsseddb.GROUP_AUTHORITIES as ga " +
                    "WHERE username = ? and g.id = gm.group_id and ga.group_id = gm.group_id " +
                    "UNION ALL " +
                    "SELECT gpc.parent_id, gpc.child_id, g.group_name, ga.authority " +
                    "FROM wsseddb.groups_parent_child as  gpc, rec_groups as rg, wsseddb.GROUPS  as g,  wsseddb.GROUP_AUTHORITIES as ga " +
                    "WHERE child_id = rg.parent and g.id = gpc.parent_id and ga.group_id = gpc.parent_id) " +
                    "SELECT gs.parent, gs.name as group_name, gs.authority FROM rec_groups gs");
    return manager;
}`

我没有回来List GrandedAuthority。当我使用postgresql时,一切运行良好。如何用h2解决这个问题?

enter image description here

在web H2中查询工作正常,但在spring jdbc preparentstatement返回0行

1 个答案:

答案 0 :(得分:2)

在H2的递归CTE实现中

There is a known bugSee also this question。现在(版本1.4.191),如果递归CTE包含任何绑定变量,则执行不正确。如果你删除并内联绑定变量(当然要小心SQL注入),它应该可以工作。

请注意,此功能为experimental in H2