2015-08-18 17 views
0

我有一個DAO方法。SQL查詢在休眠中發生異常

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 

我嘗試了不同的東西,但最終成爲一個錯誤。有人可以告訴我爲什麼這個人給我一個錯誤?

回答

0

你有額外的昏迷。

改變這一行:

"this_.kNum," + 

"this_.kNum" + 
+0

仍然給出了一個錯誤。不正確的語法靠近this_ – Jay

+0

謝謝。我也修正了另一個錯誤。 – Jay

+0

是什麼導致無法使用滾動異常執行查詢?我有同樣的問題。 –