【发布时间】:2018-12-29 18:39:56
【问题描述】:
我有实体对象:
@Entity(name = "table")
public class SomeEntity {
@Id
@Column(name = "id_column_name")
public final BigDecimal entityId;
@Column(name = "table_column_name")
public final String entityFieldName;
}
我有这样定义的数据库视图:
CREATE OR REPLACE FORCE EDITIONABLE VIEW "V_TABLE" ("ID_COLUMN_NAME", "TABLE_COLUMN_NAME", "SOME_OTHER_COLUMN") AS ... (some SQL magic)
我有自定义查询的存储库:
@RepositoryRestResource
interface SomeEntityRepository extends PagingAndSortingRepository<SomeEntity, BigDecimal> {
@Query(value = "select id_column_name, table_column_name FROM V_TABLE where some_other_column = ?#{#parameter} order by ?#{#pageable}",
countQuery = "SELECT count(*) from V_TABLE v where some_other_column = ?#{#parameter}",
nativeQuery = true)
Page<SomeEntity> findBySomeParameter(@Param("parameter") long parameter, Pageable pageable);
}
当我使用 url 请求标准数据时,一切正常:
http://localhost:8080/someEntity/search/findBySomeParameter?parameter=25&page=0&size=20
但是当我添加排序信息时它不起作用:
http://localhost:8080/someEntity/search/findBySomeParameter?parameter=25&page=0&size=20&sort=entityFieldName,asc
将抛出以下异常(我使用的是 Oracle 数据库):
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "ENTITYFIELDNAME": invalid identifier
似乎排序字段没有用@Column(name) 翻译,而是内联到SQL 查询中。
有什么方法可以翻译可分页排序,使其不使用字段名而是使用列名?
【问题讨论】:
标签: java spring-boot spring-data-jpa spring-rest