【问题标题】:HQL, PostgreSQL : Not in clause not working, syntax error at )HQL,PostgreSQL:Not in 子句不起作用,语法错误在)
【发布时间】:2017-03-06 08:58:32
【问题描述】:

我正在开发一个 Spring-MVC 应用程序,我们使用 Hibernate 作为 ORM 工具。目前,我正在尝试在 Hibernate 中使用NOT in 子句进行搜索,并为其提供List<Integer>。我收到以下错误:

错误日志:

org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"
  Position: 2082
    org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
    org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
    org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:304)
    org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
    org.hibernate.loader.Loader.getResultSet(Loader.java:2066)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1863)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
    org.hibernate.loader.Loader.doQuery(Loader.java:910)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
    org.hibernate.loader.Loader.doList(Loader.java:2554)
    org.hibernate.loader.Loader.doList(Loader.java:2540)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
    org.hibernate.loader.Loader.list(Loader.java:2365)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236)
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1300)
    org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)

HQL 查询:

 @Override
    public List<GroupNotes> searchForTextInGroup(long groupId, String text, List<Integer> canvasNotAllowedList) {
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery("from GroupNotes as gn where gn.groupId=:groupId and gn.canvasId not in (:canvasIdList) and gn.tags like :tag");
        query.setParameter("groupId", groupId);
        query.setParameter("tag", "%" + text + "%");
        query.setParameterList("canvasIdList",canvasNotAllowedList);
        return query.list();
    }

任何想法我们做错了什么?谢谢你。

更新

使用查询调试输出:

DEBUG: org.hibernate.SQL - select groupnotes0_.mnoteid as mnoteid1_25_, groupnotes0_.activeedit as activeed2_25_, groupnotes0_.canvas_id as canvas_i3_25_, groupnotes0_.collapse as collapse4_25_, groupnotes0_.copy_timestamp as copy_tim5_25_, groupnotes0_.creatorid as creatori6_25_, groupnotes0_.disabled_sort_id as disabled7_25_, groupnotes0_.end_is_milestone as end_is_m8_25_, groupnotes0_.erledigt_timestamp as erledigt9_25_, groupnotes0_.excel_data as excel_d10_25_, groupnotes0_.finished as finishe11_25_, groupnotes0_.gannt as gannt12_25_, groupnotes0_.ganntorder as ganntor13_25_, groupnotes0_.gantt_duration as gantt_d14_25_, groupnotes0_.gantt_status as gantt_s15_25_, groupnotes0_.group_id as group_i16_25_, groupnotes0_.level as level17_25_, groupnotes0_.mnotecolor as mnoteco18_25_, groupnotes0_.mnoteorder as mnoteor19_25_, groupnotes0_.mnoteheadline as mnotehe20_25_, groupnotes0_.mnotetext as mnotete21_25_, groupnotes0_.timestamp as timesta22_25_, groupnotes0_.notecreatoremail as notecre23_25_, groupnotes0_.notedate as notedat24_25_, groupnotes0_.notedisabled as notedis25_25_, groupnotes0_.noteinactive as noteina26_25_, groupnotes0_.note_milestone as note_mi27_25_, groupnotes0_.notenumber as notenum28_25_, groupnotes0_.notetarget as notetar29_25_, groupnotes0_.msectionid as msectio46_25_, groupnotes0_.pad_id as pad_id30_25_, groupnotes0_.person_who_zugweised as person_31_25_, groupnotes0_.prefix as prefix32_25_, groupnotes0_.preview_text as preview33_25_, groupnotes0_.privatenoteflag as private34_25_, groupnotes0_.privatenoteuser as private35_25_, groupnotes0_.processing as process36_25_, groupnotes0_.startDate as startDa37_25_, groupnotes0_.start_is_milestone as start_i38_25_, groupnotes0_.tags as tags39_25_, groupnotes0_.target_time as target_40_25_, groupnotes0_.task_relations as task_re41_25_, groupnotes0_.uploader as uploade42_25_, groupnotes0_.urgent as urgent43_25_, groupnotes0_.zugwisenpersonid as zugwise44_25_, groupnotes0_.zugwisen_person_timestamp as zugwise45_25_ from groupnotes groupnotes0_ where groupnotes0_.group_id=? and (groupnotes0_.canvas_id not in  ()) and (groupnotes0_.tags like ?)
DEBUG: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - could not extract ResultSet [n/a]
org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"
  Position: 2082
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:304)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)

【问题讨论】:

  • 将 Hibernate 设置为 show_sql 并发布正在执行的实际 SQL 查询。 (顺便说一句,你真的应该使用泛型。)
  • @chrylis 我在主帖中添加了调试输出,并自己检查了查询。你能告诉我我应该如何处理泛型吗?谢谢。

标签: java sql spring postgresql hibernate


【解决方案1】:
query.setParameterList("canvasIdList",canvasNotAllowedList);

可能 canvasNotAllowedList 是空的。

【讨论】:

  • 是的。而已。我认为 HQL 可以处理它,当我检查查询时,我看到它们是空的,所以必须添加一个 if 条件以避免在它为空时检查它。
  • 这不是一个写得很好的答案,但很可能是问题所在。
猜你喜欢
  • 2019-04-18
  • 2016-08-21
  • 1970-01-01
  • 2011-12-26
  • 2011-07-30
  • 2016-03-02
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多