2012-09-04 16 views
5

私はhsqldbとhibernateを使ってクエリを実行しています。私は文によって順序を持っている: -HSQLDB複数の列で並べ替える

Query query = em 
    .createQuery("SELECT DISTINCT new com.mytransferobject.QuickSearchResultTO(m.id, m.firstName||' '||m.surname, '') " + 
     "FROM Member m, ClassC c, ClassCm cm " + 
     "WHERE c.id = cm.clubId AND m.id = cm.memberId " + 
     "AND c.lft >= :lft AND c.lft <= :rgt " + 
     "AND (firstName LIKE :memberName OR surname LIKE :memberName) " + 
     "AND m.deleted = false " + 
     "AND c.deleted = false " + 
     "ORDER BY m.firstName, m.surname"); 

query.setParameter("lft", lft); 
query.setParameter("rgt", rgt); 
query.setParameter("memberName", memberName + "%"); 

これはMYSQLで正常に動作しますが、私のテストコードに失敗した(HSQLDB 2.2.6を使用して)。私はそれらを一緒に連結しようとしたが、それは正常に動作しますが、これは非常に使用されるクエリですので、私は不必要な操作を避けたいと思います。

私は例外を取得しています: - あなたは(あなたのケースでは、m.firstNameとm.surname順に列を指定するため

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: invalid ORDER BY expression 
     at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1360) 
     at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1288) 
     at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:261) 
     at com.servasport.smp.dao.jpa.MemberDaoImpl.quickSearchAll(MemberDaoImpl.java:626) 
     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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
     at $Proxy68.quickSearchAll(Unknown Source) 
     at com.servasport.smp.dao.MemberDaoTest.quickSearchSuccess(MemberDaoTest.java:768) 
     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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
     at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
     at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
     at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
     at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
     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: org.hibernate.exception.SQLGrammarException: invalid ORDER BY expression 
     at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:83) 
     at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 
     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) 
     at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:146) 
     at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) 
     at $Proxy95.prepareStatement(Unknown Source) 
     at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:147) 
     at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:166) 
     at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:145) 
     at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1720) 
     at org.hibernate.loader.Loader.doQuery(Loader.java:828) 
     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) 
     at org.hibernate.loader.Loader.doList(Loader.java:2447) 
     at org.hibernate.loader.Loader.doList(Loader.java:2433) 
     at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2263) 
     at org.hibernate.loader.Loader.list(Loader.java:2258) 
     at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470) 
     at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355) 
     at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196) 
     at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1161) 
     at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101) 
     at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:252) 
     ... 43 more 
    Caused by: java.sql.SQLSyntaxErrorException: invalid ORDER BY expression 
     at org.hsqldb.jdbc.Util.sqlException(Unknown Source) 
     at org.hsqldb.jdbc.Util.sqlException(Unknown Source) 
     at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source) 
     at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source) 
     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 org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138) 
     ... 61 more 
    Caused by: org.hsqldb.HsqlException: invalid ORDER BY expression 
     at org.hsqldb.error.Error.error(Unknown Source) 
     at org.hsqldb.error.Error.error(Unknown Source) 
     at org.hsqldb.QuerySpecification.resolveGroups(Unknown Source) 
     at org.hsqldb.QuerySpecification.resolveTypesPartTwo(Unknown Source) 
     at org.hsqldb.QueryExpression.resolve(Unknown Source) 
     at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source) 
     at org.hsqldb.ParserCommand.compilePart(Unknown Source) 
     at org.hsqldb.ParserCommand.compileStatement(Unknown Source) 
     at org.hsqldb.Session.compileStatement(Unknown Source) 
     at org.hsqldb.StatementManager.compile(Unknown Source) 
     at org.hsqldb.Session.execute(Unknown Source) 
     ... 68 more 
+0

どのように失敗しますか?すべての例外スタックトレース?あなたは何をしたいのですか?代わりに何をしますか? –

+0

あなたはどのクエリを実行していますか?失敗は何ですか? 'ORDER BY firstname、surname'はHSQLDBでサポートされています。 – fredt

+0

申し訳ありませんが、私は今例外を追加しました。私は2.2.8(最新のMaven)に更新しようとしましたが、同じ例外 –

答えて

6

はおそらく、あなたは例外「という表現によって、無効な順序」を持っています)はSELECT句で明示的に返されません。 SQL ServerやMySQLのような一部のデータベースはこれを必要としませんが、HSQLDBやDB2のようなものはSELECTのフィールドもなくこのORDER BYを実行できません。

私はあなたが使用している場合、あなたのコードが動作すると思う:

Query query = em 
    .createQuery("SELECT DISTINCT new com.mytransferobject.QuickSearchResultTO(m.id, m.firstName, m.surname, m.firstName||' '||m.surname, '') " + 
     "FROM Member m, ClassC c, ClassCm cm " + 
     "WHERE c.id = cm.clubId AND m.id = cm.memberId " + 
     "AND c.lft >= :lft AND c.lft <= :rgt " + 
     "AND (firstName LIKE :memberName OR surname LIKE :memberName) " + 
     "AND m.deleted = false " + 
     "AND c.deleted = false " + 
     "ORDER BY m.firstName, m.surname"); 
0

私は同じ問題を持っていた、私はGROUP BYの代わりに、DISTINCTを使用してそれを修正、それは同じ結果を返します。 コードを使用すると、次のコードを使用できます。

Query query = em 
.createQuery("SELECT new com.mytransferobject.QuickSearchResultTO(m.id, m.firstName||' '||m.surname, '') " + 
    "FROM Member m, ClassC c, ClassCm cm " + 
    "WHERE c.id = cm.clubId AND m.id = cm.memberId " + 
    "AND c.lft >= :lft AND c.lft <= :rgt " + 
    "AND (firstName LIKE :memberName OR surname LIKE :memberName) " + 
    "AND m.deleted = false " + 
    "AND c.deleted = false " + 
    "GROUP BY m.idORDER BY m.firstName, m.surname");