我有一个实体对象:
@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查询中。有没有办法使可分页的排序被翻译,这样它就会使用列名而不是字段名呢?