有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SQL查询在Hibernate中出现异常

我有一个刀法

    public ScrollableResults getPRecords(StatelessSession statelessSession) throws UnsupportedEncodingException{
    Criteria crit = statelessSession.createCriteria(APRecord.class, "apr");
    crit.createAlias("mAsset", "aId", Criteria.INNER_JOIN);
    SQLQuery q=statelessSession.createSQLQuery("select distinct aId1_.aId, this_.pNAME, " +
            "this_.kNum," +
            "from " +
            "AP_CPView " +
            "this_ inner join A_MAView aId1_ " +
            "on this_.agentG= aId1_.AgentG ");
    return q.scroll(ScrollMode.FORWARD_ONLY);
}

查询在SQL中运行良好,但试图在Hibernate中编写它会给我带来错误

            org.hibernate.exception.GenericJDBCException: could not execute query using scroll
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.scroll(Loader.java:2402)
at org.hibernate.loader.custom.CustomLoader.scroll(CustomLoader.java:322)
at org.hibernate.impl.StatelessSessionImpl.scrollCustomQuery(StatelessSessionImpl.java:626)
at org.hibernate.impl.AbstractSessionImpl.scroll(AbstractSessionImpl.java:170)
at org.hibernate.impl.SQLQueryImpl.scroll(SQLQueryImpl.java:205)
at com.test.hibernate.HibernateAcMDAOTest.getPatchRecords(HibernateAcMDAOTest.java:199)
at com.test.hibernate.HibernateAcMDAOTest.testFilteredPRecords(HibernateAcMDAOTest.java:344)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at org.unitils.UnitilsJUnit3.runTest(UnitilsJUnit3.java:111)
at junit.framework.TestCase.runBare(TestCase.java:134)
at org.unitils.UnitilsJUnit3.runBare(UnitilsJUnit3.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:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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)

Caused by: java.sql.SQLException: Incorrect syntax near the keyword 'from'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2816)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2254)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:631)
at net.sourceforge.jtds.jdbc.MSCursorResultSet.processOutput(MSCursorResultSet.java:943)
at net.sourceforge.jtds.jdbc.MSCursorResultSet.cursorCreate(MSCursorResultSet.java:541)
at net.sourceforge.jtds.jdbc.MSCursorResultSet.<init>(MSCursorResultSet.java:154)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:424)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:777)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1849)
at org.hibernate.loader.Loader.scroll(Loader.java:2367)
... 27 more

我尝试过不同的东西,但最终都是错误的。有人能告诉我为什么from给了我一个错误吗


共 (1) 个答案

  1. # 1 楼答案

    你额外昏迷了

    更改此行:

    "this_.kNum," +
    

    "this_.kNum" +