SQLGrammar例外,coulmn' Id'执行本机查询时找不到

时间:2016-05-12 10:40:30

标签: java mysql sql hibernate

我有以下表格:

stdqualificationmaster

QualificationId studentRegNo CourseName Percentage

1               1            A           91 
2               1            B           81
3               1            C           71
4               1            D           61
5               2            A           91
6               2            B           81
7               2            C           71
8               2            D           50

testmaster:

TestId  studentRegNo testLevel percentage

1       1            1         91 
2       1            2         81
3       1            3         71
4       2            1         80
5       2            2         99
6       2            3         87

这是我的 PlacementDaoRdbms 类,它执行上述2个表的本机SQL查询:

PlacementDaoRdbms

package com.ms.avalon.master.dao.rdbms;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import com.ms.avalon.master.beans.PlacementCaseBean;
import com.ms.avalon.master.dao.PlacementDao;
import com.ms.avalon.master.formbeans.PlacementFormBean;
import com.ms.avalon.master.pojos.studentpojos.StudentEduPojo;
import com.ms.avalon.master.pojos.studentpojos.StudentTestPojo;

public class PlacementDaoRdbms extends DBConnectionDao implements PlacementDao {

@Override
public List<StudentEduPojo> searchWithCases(PlacementCaseBean caseBean, PlacementFormBean placementBean) {

    System.out.println(session);
    System.out.println(hibernateTemplate);

    DetachedCriteria criteriaE = DetachedCriteria.forClass(StudentEduPojo.class, "qualTable");
    DetachedCriteria criteriaT = DetachedCriteria.forClass(StudentTestPojo.class, "testTable");

    String query = 
            "SELECT stdqualificationmaster.studentRegNo "
            + "FROM "
            + "(SELECT studentRegNo FROM stdqualificationmaster "
            + "WHERE (CourseName = 'High School' AND Percentage >= 90) "
            + "OR (CourseName = 'Intermediate' AND Percentage >= 80) "
            + "OR (CourseName = 'BCA' AND Percentage >= 70) "
            + "OR (CourseName = 'MCA' AND Percentage >= 60) "
            + "GROUP BY studentRegNo "
            + "HAVING COUNT(1) = 4 "
            + ") stdqualificationmaster JOIN "
            + "(SELECT studentRegNo FROM testmaster "
            + "WHERE (testLevel = '1' AND percentage >= 90) "
            + "OR (testLevel = '2' AND percentage >= 80) "
            + "OR (testLevel = '3' AND percentage >= 70) "
            + "GROUP BY studentRegNo "
            + "HAVING COUNT(1) = 3 "
            + ") testmaster ON stdqualificationmaster.studentRegNo = testmaster.studentRegNo;";

    Transaction tx = session.beginTransaction();
    SQLQuery q = session.createSQLQuery(query)
    .addEntity(StudentEduPojo.class);

    List<StudentEduPojo> list = q.list();
    System.out.println(list.size());

    return null;
}

}

好吧,基本上SQL查询搜索studentRegNo,它匹配两个表中的最小条件(具有最低限度和测试百分比)。查询执行后,抛出以下异常。

  

Hibernate:SELECT * FROM(SELECT studentRegNo FROM stdqualificationmaster WHERE(CourseName =&#39; High School&#39; AND Percentage&gt; = 90)OR(CourseName =&#39; Intermediate&#39; AND Percentage&gt; = 80)或(CourseName =&#39; BCA&#39; AND Percentage&gt; = 70)OR(CourseName =&#39; MCA&#39; AND Percentage&gt; = 60)GROUP BY studentRegNo HAVING COUNT(1)= 4 )stdqualificationmaster JOIN(SELECT studentRegNo FROM testmaster WHERE(testLevel =&#39; 1&#39; AND percentage&gt; = 90)或(testLevel =&#39; 2&#39; AND percentage&gt; = 80)OR(testLevel = &#39; 3&#39; AND百分比&gt; = 70)GROUP BY studentRegNo HAVING COUNT(1)= 3)testmaster ON stdqualificationmaster.studentRegNo = testmaster.studentRegNo;

     

2016年5月12日下午3:37:50 org.apache.catalina.core.StandardWrapperValve   在上下文中为servlet [dispatcher]调用SEVERE:Servlet.service()   与路径[/Avalon-1.0]抛出异常[请求处理失败;   嵌套异常是 org.hibernate.exception.SQLGrammarException:   无法执行查询]的根本原因java.sql.SQLException:Column   &#39; QualificationId&#39;没找到。 at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)at at   com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1136)at at   com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2777)at at   org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:236)     在org.hibernate.type.IntegerType.get(IntegerType.java:28)at   org.hibernate.type.NullableType.nullSafeGet(NullableType.java:113)at   org.hibernate.type.NullableType.nullSafeGet(NullableType.java:102)at at   org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1088)at   org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:553)at   org.hibernate.loader.Loader.doQuery(Loader.java:689)at   org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)     在org.hibernate.loader.Loader.doList(Loader.java:2144)处   org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028)at at   org.hibernate.loader.Loader.list(Loader.java:2023)at   org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)     在   org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)     在   org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)     在org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:150)at at   com.ms.avalon.master.dao.rdbms.PlacementDaoRdbms.searchWithCases(PlacementDaoRdbms.java:107)     在   com.ms.avalon.master.business.TalentCaseHandler.searchForCases(TalentCaseHandler.java:63)     在   com.ms.avalon.master.service.impl.PlacementServiceImpl.searchForTalent(PlacementServiceImpl.java:31)     在   com.ms.avalon.master.controllers.PlacementController.searchForTalent(PlacementController.java:29)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)     在   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)     在   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)     在   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)     在   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)     在   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)     在   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:646)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:727)at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在   com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)     在   com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)     在   org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:611)     在   org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:316)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(未知来源)

现在,我不确定为什么它首先要查找QualificationId列,即使它是,它为什么找不到它?

1 个答案:

答案 0 :(得分:0)

我的猜测是别名会造成严重破坏。

不确定2 GROUP BY是否有效,但以下似乎是可行的,甚至可能是AND EXISTS( .... s.studentRegNo = studentRegno)

String query = 
        "SELECT studentRegNo FROM stdqualificationmaster s "
        + "WHERE (CourseName = 'High School' AND Percentage >= 90) "
        + "OR (CourseName = 'Intermediate' AND Percentage >= 80) "
        + "OR (CourseName = 'BCA' AND Percentage >= 70) "
        + "OR (CourseName = 'MCA' AND Percentage >= 60) "
        + "GROUP BY studentRegNo "
        + "HAVING COUNT(1) = 4 "
        + "AND "
        + "studentRegNo IN (SELECT studentRegNo FROM testmaster "
        + "WHERE (testLevel = '1' AND percentage >= 90) "
        + "OR (testLevel = '2' AND percentage >= 80) "
        + "OR (testLevel = '3' AND percentage >= 70) "
        + "GROUP BY studentRegNo "
        + "HAVING COUNT(1) = 3 "
        + ")";