2

我的查询是。

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();

此查询不起作用..请帮助...

问题表

答案表

HTTP 状态 500 - 请求处理失败;嵌套异常是 org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: , 第 1 行附近第 38 列 [DELETE com.online.test.model.Question, Answer FROM com.online.test.model.Question que LEFT加入回答 ans ON que.id=ans.questionId WHERE que.quiz_type_id=:qtypeid]

类型异常报告

消息请求处理失败;嵌套异常是 org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: , 第 1 行附近第 38 列 [DELETE com.online.test.model.Question, Answer FROM com.online.test.model.Question que LEFT加入回答 ans ON que.id=ans.questionId WHERE que.quiz_type_id=:qtypeid]

描述 服务器遇到一个内部错误,阻止它完成这个请求。

例外

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: , 第 1 行附近第 38 列 [DELETE com.online.test.model.Question, Answer FROM com.online.test.model.Question que LEFT加入回答 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, Answer FROM com.online.test.model.Question que LEFT JOIN Answer 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(Native Method) 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(Unknown Source) com.online.test.AdminController.removecountry(AdminController.java:59) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 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)

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

4

2 回答 2

5
于 2016-02-10T11:47:53.917 回答
2

您不能在 HQL 中使用onwith 。正如@DraganBozanovic 建议的那样,join您不能用于删除。join有效的查询可以是

DELETE FROM Question que WHERE que.quiz_type_id = :qtypeid

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

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

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

CascadeType.ALL您可以考虑使用更具体的级联,而不是使用。

于 2016-02-10T12:00:42.760 回答