HIbernate 标准限制.in
我在使用 Restrictions.in 时遇到 Hibernate Criteria 问题。如果发送到限制的列表中没有任何值,那么它会抛出 SQL GrammerException,这是预期的,因为查询看起来像这样
从人员 p 中选择 *,其中 () 中的 p.id
Hibernate 日志上的错误---
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ')'. at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError
SQLServerException.java:197)
Spring 日志上的错误...
org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.loader.Loader.doList(Loader.java:2536)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276
org.hibernate.loader.Loader.list(Loader.java:2271
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
导致它的代码是
criteria.add(Restrictions.in("id", idList));
我尝试这样做,但它将返回表中的所有值,这与我想要的完全相反..当列表大小为 0 时不返回任何内容..
if(idList.size()>0
criteria.add(Restrictions.in("id", idList));
那么我应该在这里进行哪些更改,以便当列表中没有任何值时我不会从查询中得到任何结果例外情况...提前致谢!
I am having an issue with Hibernate Criteria when using Restrictions.in.. If the list sent to the restriction doesn't have any values in it then it throws SQL GrammerException which is expected because query would look something like
select * from Person p where p.id in ()
error on Hibernate logs---
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ')'. at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError
SQLServerException.java:197)
error on Spring logs...
org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.loader.Loader.doList(Loader.java:2536)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276
org.hibernate.loader.Loader.list(Loader.java:2271
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
And the code that is causing it is
criteria.add(Restrictions.in("id", idList));
I tried doing this, but it will return all the values in the table, which is completely opposite of what I want.. when the list size is 0 don't return anything..
if(idList.size()>0
criteria.add(Restrictions.in("id", idList));
So what changes should I make here so that I get no results from the query when list doesn't have any values in it instead of the exceptions... Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将整个条件包含在 if-then 语句中。如果 idList 为 null 或 idList.size() == 0,则无需运行查询。
Wrap the entire Criteria in an if-then statement. If the
idList
is null oridList.size() == 0
, don't even bother running the query.