在 Spring JPARepository 中,如何选择不同的列值而不是不同的实体?

问题描述 投票:0回答:1

我正在使用 Spring Boot 2.5 和 JPA 2.1。我有以下实体,映射到 DB2 表

@Entity
@Table(name="XYZVVV00")
public class MyProduct {
   ...
   @Column(nullable=false)
   private String prodType;

}

我有以下存储库...

public interface MyProductRepository extends JpaRepository<MyProduct, Long> {
    ...
    @Query(value="SELECT DISTINCT PROD_TYPE FROM XYZVVV00", nativeQuery=true)
    public List<String> findDistinctProdTypes();

}

有没有一种方法可以在不使用本机查询的情况下编写 Spring 代码?

spring spring-boot jpa spring-data-jpa db2
1个回答
0
投票

要在

Distinct
中使用
@Query
,我怎么知道如果不启用本机查询就不能使用。但您可以将其替换为命名查询,如下所示:

List<String> findDistinctByProdType();
© www.soinside.com 2019 - 2024. All rights reserved.