使用sql IN的Hibernate的Stackoverflow(id,id,id,id..id)

时间:2014-08-14 14:02:32

标签: java mysql sql hibernate stack-overflow

我收到下面的错误,说有堆栈溢出。发生这种情况是因为带有IN(id,id,id ... id)的sql语句有大量参数。有没有什么办法解决这一问题?这种情况发生在Eclipse的本地环境中。

JPA

@Query(value="SELECT p FROM PendingCourseRegistration p WHERE p.sisId IN ?1 AND p.testId = ?2")
List<PendingCourseRegistration> findPendingCourseRegistrationInSisIdsAndTestId(List<String> sisIds, Long testID);

错误

java.lang.StackOverflowError: null
    at java.lang.Abstract witingBuilder.append(AbstractStringBuilder.java:416) ~[na:1.7.0_17]
    at java.lang.StringBuffer.append(StringBuffer.java:237) ~[na:1.7.0_17]
    at antlr.BaseAST.toStringList(BaseAST.java:341) ~[antlr-2.7.7.jar:na]
    at antlr.BaseAST.toStringList(BaseAST.java:347) ~[antlr-2.7.7.jar:na]
    at antlr.BaseAST.toStringList(BaseAST.java:347) ~[antlr-2.7.7.jar:na]
    at antlr.BaseAST.toStringList(BaseAST.java:347) ~[antlr-2.7.7.jar:na]

Hibernate查询

2:26.763 [ocPifScheduler-1] DEBUG o.s.o.j.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler - Creating new EntityManager for shared EntityManager invocation
09:52:26.788 [Scheduler-1] DEBUG org.hibernate.hql.internal.ast.QueryTranslatorImpl - parse() - HQL: SELECT p FROM com.test.PendingCourseRegistration p WHERE p.sisId IN (:x10_, :x11_, :x12_, :x13_, :x14_, :x15_, :x16_, :x17_, :x18_, :x19_, :x110_, :x111_, :x112_, :x113_, :x114_, :x115_, :x116_, :x117_, :x118_, :x119_, ...:xN) AND p.id = ?2
09:52:26.891 [Scheduler-1] DEBUG org.hibernate.hql.internal.ast.QueryTranslatorImpl - --- HQL AST ---
 \-[QUERY] Node: 'query'
    +-[SELECT_FROM] Node: 'SELECT_FROM'
    |  +-[FROM] Node: 'FROM'
    |  |  \-[RANGE] Node: 'RANGE'
    |  |     +-[DOT] Node: '.'
    |  |     |  +-[DOT] Node: '.'
    |  |     |  |  +-[DOT] Node: '.'
    |  |     |  |  |  +-[DOT] Node: '.'
    |  |     |  |  |  |  +-[DOT] Node: '.'
    |  |     |  |  |  |  |  +-[DOT] Node: '.'
    |  |     |  |  |  |  |  |  +-[IDENT] Node: 'com'
    |  |     |  |  \-[IDENT] Node: 'model'
    |  |     |  \-[IDENT] Node: 'PendingCourseRegistration'
    |  |     \-[ALIAS] Node: 'p'
    |  \-[SELECT] Node: 'SELECT'
    |     \-[IDENT] Node: 'p'
    \-[WHERE] Node: 'WHERE'
       \-[AND] Node: 'AND'
          +-[IN] Node: 'in'
          |  +-[DOT] Node: '.'
          |  |  +-[IDENT] Node: 'p'
          |  |  \-[IDENT] Node: 'sisId'
          |  \-[IN_LIST] Node: 'inList'
          |     +-[COLON] Node: ':'
          |     |  \-[IDENT] Node: 'x10_'
          |     +-[COLON] Node: ':'
          |     |  \-[IDENT] Node: 'x11_'
          |     +-[COLON] Node: ':'
          |     |  \-[IDENT] Node: 'x12_'
          |     +-[COLON] Node: ':'
          |     |  \-[IDENT] Node: 'x13_'
          |     +-[COLON] Node: ':'
          |     |  \-[IDENT] Node: 'x14_'
          |     +-[COLON] Node: ':'
          |     |  \-[IDENT] Node: 'x15_'
          |     +-[COLON] Node: ':'
          |     |  \-[IDENT] Node: 'x16_'
          |     +-[COLON] Node: ':'
          |     |  \-[IDENT] Node: 'x17_'

2 个答案:

答案 0 :(得分:0)

在使用Hibernate 4.3.1.Final的Grails项目(2.3.6)中,我们从未遇到过该错误,但由于查询缓冲区大小限制我们遇到了另一个错误:

由于您使用的是in (?, ..., ?),因此您列出的内容与列表中的项目一样多?,,这意味着对于大型列表(50000表示),您可以编写100000个字符的查询,并且您可能有这个例外(这里有pgSQL驱动程序):

Foobar.executeQuery("select f from Foobar f where f.id in (:ids)", 
                 [ids: 1L..100000L]); // Groovy way of creating a list of 100000 items.

错误:

SqlExceptionHelper:146 - An I/O error occured while sending to the backend. 
SqlExceptionHelper:146 - This connection has been closed.

这就是为什么我认为你可能需要

  1. 将您的ID列表拆分为较小的列表(如500项左右)并手动合并结果,这比您正在使用的默认弹簧数据要做的更多。

  2. 将id列表存储到临时表中(使用JPA可能会证明这是一项痛苦的任务)。临时表将是(key,sids)的元组。你会生成一个临时密钥(对于会话),使用密钥批量插入id到该表,刷新以便hibernate将更改推送到数据库,使用子表使用该子查询(p.sidIds in (select sisIds from IdTable where key = ?1)),删除数据从那张桌子。 虽然这是一项痛苦的任务,但它可能会提升表现。

答案 1 :(得分:0)

我无法使用Hibernate 4.2.4复制此内容 你能提供你正在使用的Hibernate版本吗? 我建议的另一件事是尝试在&#34; antlr.BaseAST.toStringList()&#34;上设置一个断点。执行方法时,查看运行时变量和调用antlr方法的方法并导致循环

希望有所帮助