【问题标题】:Spring JPA in clause throwing Operand should contain 1 column(s)子句中的 Spring JPA 抛出操作数应包含 1 列
【发布时间】:2018-12-16 02:13:58
【问题描述】:

我正在使用 Spring JPA 对 MySql 数据库进行选择操作

表格

select id, grp, name from student;

存储库方法

List<Student> findByGrpAndName(String grp, Set<String> name);

它正在抛出

java.sql.SQLException: Operand should contain 1 column(s) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)

我尝试调试休眠代码以查看创建的查询。并且查询有语法错误。

select student0_.name as name1_1_, student0_.grp as grp2_1_ from student student0_ where student0_.grp=? and student0_.name=(? , ?);

应该是student0_.name in (? , ?);

我是否遗漏了什么要告诉 JPA 它应该是 in clause

【问题讨论】:

  • findByGrpAndName方法的实现是什么?
  • 在 JPA 中,存储库函数不需要实现。它遵循一些命名约定来检测这些函数应该返回什么。进一步阅读:stackoverflow.com/questions/40330369/…

标签: java mysql hibernate spring-boot spring-data-jpa


【解决方案1】:

你已经接近了!您可以使用关键字In 作为Name 子句的限定符(如in the documentation 所述)。您需要做的就是将您的查询修改为:

List<Student> findByGrpAndNameIn(String grp, Set<String> name);

希望有帮助!

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多