Andrea Robinson Andrea Robinson - 4 months ago 16
Java Question

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

I am trying to retrieve all rows where emailAccess is equal to john@yahoo.com

user table is structured this way

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


I have a log table like this

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


now I am using the following hql query to retrieve the log based on the userId where emailAccesss from sesssion is 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();


trying to use the above hql query gives me this error

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)


second stacktrace

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


Where am I failing.
Kindly assist!

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

This returns more than one row and you have:

WHERE c.user =

You can't have an equals with multiple results on the right. Either change your query to return a single row or change to be something like:

WHERE c.user in