【问题标题】:Collections Jpa Spring Data return No value specified for parameter 1集合 Jpa Spring 数据返回没有为参数 1 指定值
【发布时间】:2018-01-18 04:31:38
【问题描述】:

我正在使用 jpa,我有以下实体:

@ManyToMany
@JoinTable(
        name = "users_roles",
        joinColumns = @JoinColumn(
                name = "user_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(
                name = "role_id", referencedColumnName = "rid"))
private Collection<UserRole> roles;

这是我的规范过滤器

if (!filter.getRoles().isEmpty()) {

        System.out.println("Append roles");
        predicates.add(cb.equal(root.get("roles"), filter.getRoles()));
    }

我想做的是根据过滤器中给出的角色进行查询。我在用户个人资料实体中有角色集合,我的搜索是在用户个人资料实体上。

这就是 spring 显示我的错误。

java.sql.SQLException: No value specified for parameter 1
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

当涉及到集合查询时,我有点困惑。

【问题讨论】:

  • 您尚未发布查询执行代码的其余部分,例如您在查询中定义参数的位置以及设置该参数值的位置。此外,您不能在等式过滤器中使用多值字段!

标签: jpa collections spring-data-jpa criteria-api


【解决方案1】:

这是我一直在寻找的答案的更新。也许它会帮助其他人,而不是对有效问题给出负面评价。

   if (filter.getRoles() != null) {
    UserRole role = filter.getRoles().get(0);
    System.out.println("Append roles");
    predicates.add(cb.equal(root.join("roles").get("roleName"), role.getRoleName()));
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    相关资源
    最近更新 更多