hql查询删除两个表

时间:2016-02-10 10:59:31

标签: hibernate hql hql-delete

  

我的查询是。

Query query1 = session.createQuery(
                "DELETE Question, Answer FROM Question que LEFT JOIN Answer ans ON que.id=ans.questionId  WHERE que.quiz_type_id=:qtypeid");
        query1.setParameter("qtypeid", id);
        query1.executeUpdate();

  

此查询无效..请帮助...

question table

answer table

  

HTTP状态500 - 请求处理失败;嵌套异常是org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:,在第1行附近,第38列[DELETE com.online.test.model.Question,回答来自com.online.test.model.Question que LEFT JOIN回答ans ON que.id = ans.questionId WHERE que.quiz_type_id =:qtypeid]

输入例外报告

消息请求处理失败;嵌套异常是org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:,在第1行附近,第38列[DELETE com.online.test.model.Question,回答来自com.online.test.model.Question que LEFT JOIN回答ans ON que.id = ans.questionId WHERE que.quiz_type_id =:qtypeid]

说明服务器遇到内部错误,导致无法完成此请求。

例外

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.hibernate.hql.internal.ast.QuerySyntaxException:意外标记:,第1行附近,第38列[DELETE com.online.test.model.Question,Answer from com.online.test.model.Question que LEFT JOIN回答ans ON que.id = ans.questionId WHERE que.quiz_type_id =:qtypeid]     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)     javax.servlet.http.HttpServlet.service(HttpServlet.java:618)     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)     javax.servlet.http.HttpServlet.service(HttpServlet.java:725)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

根本原因

org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:,靠近第1行,第38列[DELETE com.online.test.model.Question,回答来自com.online.test.model.Question que LEFT JOIN回答ans ON que.id = ans.questionId WHERE que.quiz_type_id =:qtypeid]     org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)     org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)     org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)     org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)     org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)     org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:126)     org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:88)     org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)     org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)     org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)     org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)     com.online.test.Dao.AdminTestDaoImpl.removeQuiz(AdminTestDaoImpl.java:104)     com.online.test.service.AdminTestServicesImpl.removeQuiz(AdminTestServicesImpl.java:58)     sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     java.lang.reflect.Method.invoke(Method.java:606)     org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)     org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)     org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)     org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:98)     org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)     org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)     org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)     org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)     com.sun.proxy。$ Proxy35.removeQuiz(未知来源)     com.online.test.AdminController.removecountry(AdminController.java:59)     sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     java.lang.reflect.Method.invoke(Method.java:606)     org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)     org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)     org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)     org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)     javax.servlet.http.HttpServlet.service(HttpServlet.java:618)     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)     javax.servlet.http.HttpServlet.service(HttpServlet.java:725)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

注意Apache Tomcat / 8.0。9日志中提供了根本原因的完整堆栈跟踪。 Apache Tomcat / 8.0.9

2 个答案:

答案 0 :(得分:4)

Hibernate批量DML操作不支持联接。来自documentation

  

Section 16.4, “Forms of join syntax”,隐式或   显式,可以在批量HQL查询中指定。子查询可以   在where子句中使用,子查询本身可以包含   联接。

您可以使用两个{{1}}语句分别删除答案和问题。

答案 1 :(得分:2)

您无法在HQL中将onjoin一起使用。并且你不能使用join进行删除,正如@DraganBozanovic建议的那样。有效查询可以是

DELETE FROM Question que WHERE que.quiz_type_id = :qtypeid

要删除相应问题时删除答案,您需要具有注释属性 - cascade = CascadeType.ALLorphanRemoval = true

如果您使用一对多关联,则可以

@OneToMany(mappedBy = "question", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Answer> answers;

您可以考虑使用更具体的级联来代替使用CascadeType.ALL