0

我试图查询:

 @Query(value = "SELECT * FROM BookingOffice WHERE tripId in (select Trip.tripId from Trip Where Trip.desfination like ?#{#desfination})   ORDER BY ?#{#pageable}",
			countQuery = "SELECT COUNT(*) FROM BookingOffice WHERE tripId in (select Trip.tripId from Trip Where Trip.desfination like ?#{#desfination})",
			nativeQuery = true)
	public Page<BookingOffice> findAllByTrip(String desfination, Pageable pageable);

我的页面请求:

Sort sort = new Sort(Direction.DESC, "officeId");
PageRequest pageRequest = new PageRequest(page, 7, sort);

运行时的休眠查询

**> 休眠:

SELECT
    * 
FROM
    BookingOffice 
WHERE
    tripId in (
        select
            Trip.tripId 
        from
            Trip 
        Where
            Trip.desfination like ?
    )   
ORDER BY
    ?,
    officeId desc offset 0 rows fetch next ? rows only**

错误:2020 年 5 月 17 日下午 12:41:29 org.apache.catalina.core.StandardWrapperValve 调用严重:servlet [dispatcher] 的 Servlet.service() 在路径 [/CarPark] 的上下文中抛出异常 [请求处理失败;嵌套异常是 org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause com.microsoft.sqlserver.jdbc.SQLServerException: ORDER BY number 1 标识的 SELECT 项包含一个变量作为标识列的表达式的一部分位置。只有在通过引用列名的表达式进行排序时才允许使用变量。 在 com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:254) 在 com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1608) 在 com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement (SQLServerPreparedStatement.java:578) 在 com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:508) 在 com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7240) 在 com。 microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2869) 在 com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:243) 在 com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement. java:218) 在 com.microsoft。org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60) 中的 org.hibernate.loader.Loader.getResultSet(Loader.java: 中的 sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:434) 2171) 在 org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1934) 在 org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1896) 在 org.hibernate.loader.Loader.doQuery(Loader.java :936) org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:339) org.hibernate.loader.Loader.doList(Loader.java:2693) org.hibernate.loader.Loader.doList(Loader. java:2676) 在 org.hibernate.loader.custom.CustomLoader 的 org.hibernate.loader.Loader.list(Loader.java:2505) 的 org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2510)。list(CustomLoader.java:338) at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2262) at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1069) at org.hibernate.query.internal .NativeQueryImpl.doList(NativeQueryImpl.java:173) 在 org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1529) 在 org.hibernate.query.Query.getResultList(Query.java:165) 在太阳。 reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(方法.java:498) ....java:2262) 在 org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:173) 在 org.hibernate.query.internal.AbstractProducedQuery 的 org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1069) .list(AbstractProducedQuery.java:1529) 在 org.hibernate.query.Query.getResultList(Query.java:165) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java :62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) ....java:2262) 在 org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:173) 在 org.hibernate.query.internal.AbstractProducedQuery 的 org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1069) .list(AbstractProducedQuery.java:1529) 在 org.hibernate.query.Query.getResultList(Query.java:165) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java :62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) ....java:173) 在 org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1529) 在 org.hibernate.query.Query.getResultList(Query.java:165) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 。 ...java:173) 在 org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1529) 在 org.hibernate.query.Query.getResultList(Query.java:165) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 。 ...在 java.lang.reflect.Method.invoke(Method.java:498) 处调用(DelegatingMethodAccessorImpl.java:43) ....在 java.lang.reflect.Method.invoke(Method.java:498) 处调用(DelegatingMethodAccessorImpl.java:43) ....

**我的数据库**

请帮我!谢谢

4

2 回答 2

0

我认为问题在于您正在使用 Pageable 对象按您的选择查询的子句排序。它需要一个列名来对结果进行排序。

于 2020-05-16T17:10:41.470 回答
0

请尝试如下,我在我的项目中做了同样的实现,它工作正常。

@Query(value = "SELECT * FROM BookingOffice WHERE tripId in (select Trip.tripId from Trip Where Trip.desfination like %?1%}))",
            nativeQuery = true)
public Page<BookingOffice> findAllByTrip(String desfination, Pageable pageable);
于 2020-05-17T13:21:57.100 回答