Caused by: java.sql.SQLException: Subquery returns more than 1 row on all rows where emailAccess is thesame

Caused by: java.sql.SQLException: Subquery returns more than 1 row on all rows where emailAccess is thesame

我正在尝试检索 emailAccess 等于 john@yahoo.com

的所有行

用户table是这样构造的

id | name | email         | emailAccess
1  | john |john@yahoo.com | john@yahoo.com
2  | jeff |jeff@yahoo.com | john@yahoo.com

我有一个这样的日志table

id | userId | message 
1  | 1      | bla bla
2  | 2      | 1234

现在我正在使用以下 hql 查询来检索基于 userId 的日志,其中来自会话的 emailAccesss 是 john@yahoo.com

String hql = "FROM Chat c WHERE c.user = (FROM User u WHERE u.emailAccess = :emailAccess)";

        return  _sessionFactory.getCurrentSession().createQuery(hql).setParameter("emailAccess", emailAccess).list();

尝试使用上述 hql 查询时出现此错误

Caused by: org.hibernate.exception.DataException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:135)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)

第二个堆栈跟踪

Caused by: java.sql.SQLException: Subquery returns more than 1 row
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)

我哪里失败了。 请协助!

(FROM User u WHERE u.emailAccess = :emailAccess)

这 return 不止一行,您有:

WHERE c.user =

右边不能有多个结果的等号。要么将您的查询更改为 return 单行,要么更改为:

WHERE c.user in

使用 hql 可以访问对象及其属性,试试这个查询:

String hql = "FROM Chat c WHERE c.user.emailAccess = :emailAccess"