我是冬眠的新手,但是对于这种情况我通常会做一个粗略的想法,我不能说这正在解决。我正在尝试创建一个过滤系统,使您可以在这种情况下与用户对不同的客户端进行分类。我的SQL查询工作正常:
SELECT * FROM [de_user_site] WHERE [deactivation_time] = '9999-12-31 00:00:00.000' AND [user_id] IN (SELECT [user_id] FROM [de_users] WHERE [client_id] = 1)
并且我试图将其转换为Java查询:
queryStr = "SELECT e FROM DeSiteUser e " + queryStr;
String queryStr = " WHERE e.deactivationTime = '9999-12-31 00:00:00.000' ";
if (clientId != null) {
queryStr += String.format("AND e.userId IN (SELECT id FROM DeUser u WHERE ");
if (clientId != null) {
queryStr += String.format("u.clientId = %d ", clientId);
}
}
但是,我对此有疑问:
Internal Server Error [#500]: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found 'null' near line 1, column 214 [SELECT e FROM com.velatt.dartentitlements.domain.DeSiteUser e WHERE e.deactivationTime = '9999-12-31 00:00:00.000' AND e.userId IN (SELECT id FROM com.velatt.dartentitlements.domain.DeUser u WHERE u.clientId = 6 ]
有人知道我忘记了什么吗,我的SQL查询是正确的,但是我不明白为什么对于Java查询它没有解决?
答案 0 :(得分:0)
您错过了大括号。错误说明了一切。
SELECT e FROM com.velatt.dartentitlements.domain.DeSiteUser e WHERE e.deactivationTime = '9999-12-31 00:00:00.000' AND e.userId IN (SELECT id FROM com.velatt.dartentitlements.domain.DeUser u WHERE u.clientId = 6
您的代码应关闭用大括号括起来
queryStr = "SELECT e FROM DeSiteUser e " + queryStr;
String queryStr = " WHERE e.deactivationTime = '9999-12-31 00:00:00.000' ";
if (clientId != null) {
queryStr += String.format("AND e.userId IN (SELECT id FROM DeUser u WHERE ");
queryStr += String.format("u.clientId = %d ", clientId);
queryStr += " ) ";
}
答案 1 :(得分:0)
请在下面替换您给定的代码。您错过了添加')'作为子查询的和。
queryStr = "SELECT e FROM DeSiteUser e " + queryStr;
String queryStr = " WHERE e.deactivationTime = '9999-12-31 00:00:00.000' ";
if (clientId != null) {
queryStr += String.format("AND e.userId IN (SELECT id FROM DeUser u WHERE ");
if (clientId != null) {
queryStr += String.format("u.clientId = %d )", clientId);
}
}