引起:java.sql.SQLException:子查询在emailAccess相同的所有行上返回多行

时间:2016-07-27 16:14:32

标签: java sql hibernate spring-boot hql

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

的所有行

用户表以这种方式构建

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

我有一个像这样的日志表

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

现在我使用以下hql查询来检索基于userId的日志,其中来自sesssion的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)

我在哪里失败了。 请帮助!

2 个答案:

答案 0 :(得分:1)

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

这会返回多行,您有:

WHERE c.user =

你不能在右边有一个等于多个结果的等号。更改查询以返回单行或更改为:

WHERE c.user in

答案 1 :(得分:1)

使用hql可以访问对象及其属性,请尝试以下查询:

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