Spring JPA。在sql select中强制转换@Entity字段

Spring JPA。在sql select中强制转换@Entity字段,spring,jpa,spring-data-jpa,Spring,Jpa,Spring Data Jpa,我拥有jpa实体: @Entity @Table(name = "document") public class Document ... @Column private String docNumber; ... 并使用JpaSpecificationExecutor从数据库获取结果: Page<Document> findAll(@Nullable Specification<Document> spec, Pageable pageable)

我拥有jpa实体:

@Entity
@Table(name = "document")
public class Document
...
@Column
private String docNumber;
...
并使用JpaSpecificationExecutor从数据库获取结果:

Page<Document> findAll(@Nullable Specification<Document> spec, Pageable pageable)
我是否可以将select列的jpa实体调整为强制转换为int(完全可以调整为其他实体字段):


这是需要的,因为在许多例子中,我得到了一个sql查询,其中
select distinct
order by cast(document0.“doc\u number”作为int4)一起使用
,PostgreSQL返回错误:对于select distinct,order by表达式必须出现在select列表中。

因为
spring data jpa
使用Hibernate作为jpa引擎,您可以使用Hibernate的注释:

@Formula("cast('doc_number' as int4)")
private int docNumberAsInt4;

但是我还没有测试它。

不,如果您想返回实体,这是不可能的。在这种情况下,您真的需要实体还是DTO可以工作?
select cast(document0_."doc_number" as int4) from document
@Formula("cast('doc_number' as int4)")
private int docNumberAsInt4;