在Hibernate中批量删除时查询语法错误

Tad*_*pec 2 java hibernate hql

我尝试使用Hibernate HQL查询执行批量删除,遵循引用manunal但我得到查询语法错误.这条线

final Query qry = getSession(false).createQuery(" delete from NewCalendarDay");
Run Code Online (Sandbox Code Playgroud)

导致异常:

org.hibernate.QueryException:query必须以SELECT或FROM开头:delete [从pl.com.bms.avaro.staticData.model.NewCalendarDay删除]

执行代码的类扩展org.springframework.orm.hibernate3.support.HibernateDaoSupport,因此getSession()方法应该返回org.hibernate.Session.我使用spring v.2.5.5和Hibernate v.3.2.6.ga

我应该配置什么能够批量删除或什么?

编辑
Bozho要求完整的堆栈跟踪,所以这里:

org.hibernate.QueryException: query must begin with SELECT or FROM: delete [ delete from pl.com.bms.avaro.staticData.model.NewCalendarDay ]
    at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:83)
    at org.hibernate.hql.classic.PreprocessingParser.token(PreprocessingParser.java:108)
    at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:28)
    at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:216)
    at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:185)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
    at pl.com.bms.avaro.staticData.dao.implementations.NewCalendarDayDaoImpl.deleteCalendarDaysForYear(NewCalendarDayDaoImpl.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy43.deleteCalendarDaysForYear(Unknown Source)
    at pl.com.bms.avaro.staticData.dao.tests.TestCalendarDayDaoImpl.testFindByDateCal(TestCalendarDayDaoImpl.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:91)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Run Code Online (Sandbox Code Playgroud)

Boz*_*zho 8

摆脱领先的空白开始.然后使用执行查询query.executeUpdate()

然后,即使这对您有用,我也不建议使用HQL删除实体.如果他们有任何关系,这些将不会被级联,你最终会有不一致的地方.

选择所有对象并session.remove(entity)在每个对象上调用.当然,如果您没有任何x-to-x关系,则可以继续使用HQL,但是如果将来添加它们,请务必小心.

更新:你可能在你的休眠配置中有这样的东西:

<prop
 key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</prop>
Run Code Online (Sandbox Code Playgroud)

它使Hibernate使用经典的解析器.删除它,然后再试一次.