查询中的 JPA 似乎没有返回任何结果

Posted

技术标签:

【中文标题】查询中的 JPA 似乎没有返回任何结果【英文标题】:JPA in query does not seem to return any result 【发布时间】:2021-05-25 15:29:02 【问题描述】:

我正在尝试运行 in 查询 (springboot+jpa+mysql)。我启用了调试日志,查询似乎没问题,但是 spring jpa 没有返回任何结果。

这里是配置:

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

     <dependencies>
        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

这是我的存储库:

@Query(value = "select * from targeting_locations where targeting_location_master_id in (:targetingLocationMasterIds)", nativeQuery = true)
List<TargetingLocations> findAllByTargetingLocationMasterIdIn(@Param("targetingLocationMasterIds") List<Long> targetingLocationMasterIds);

// 或

@Query("SELECT t FROM TargetingLocations t WHERE t.targetingLocationMasterId IN :targetingLocationMasterIds")
    List<TargetingLocations> findAllByTargetingLocationMasterIdIn(@Param("targetingLocationMasterIds") List<Long> targetingLocationMasterIds);

这里需要注意的是,'in'查询中的列不是主键。

@Entity
@Table(name = "targeting_locations")
public class TargetingLocations 

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;

    @Column(name = "geohash")
    public String geoHash;

    @Column(name = "site_name")
    public String siteName;

    @Column(name = "city")
    public String city;

    @Column(name = "country")
    public String country;

    @Column(name = "state")
    public String state;

    @Column(name = "latitude")
    public Double latitude;

    @Column(name = "longitude")
    public Double longitude;

    @Column(name = "targeting_type")
    public String targetingType;

    @Column(name = "targeting_data")
    public String targetingData;

    @Column(name = "targeting_location_master_id")
    public Long targetingLocationMasterId; // ----> in query in this column

查询生成:

Hibernate: 
    select
        targetingl0_.id as id1_1_,
        targetingl0_.city as city2_1_,
        targetingl0_.country as country3_1_,
        targetingl0_.geohash as geohash4_1_,
        targetingl0_.latitude as latitude5_1_,
        targetingl0_.longitude as longitud6_1_,
        targetingl0_.site_name as site_nam7_1_,
        targetingl0_.state as state8_1_,
        targetingl0_.targeting_data as targetin9_1_,
        targetingl0_.targeting_location_master_id as targeti10_1_,
        targetingl0_.targeting_type as targeti11_1_ 
    from
        targeting_locations targetingl0_ 
    where
        targetingl0_.targeting_location_master_id in (
            ?
        )
2021-02-23 16:22:46.199 TRACE 28282 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [13063]

【问题讨论】:

您是否运行过上述查询。是返回结果吗?尝试 findAllByTargetingLocationMasterIdList 而不是 findAllByTargetingLocationMasterIdIn 或检查***.com/questions/18987292/… 您可以尝试使用Is 代替In 并打开单个元素集合,看看id 是否真的匹配? 【参考方案1】:

只需在您的存储库中将其定义为方法即可。

List<TargetingLocations> findByTargetingLocationMasterIdIn(List<Long> targetingLocationMasterIds);

删除 @Query 并且名称应以 findBy... 开头,而不是 findAll...

你想要实现的东西会被spring-jpa自动实现和实现

【讨论】:

以上是关于查询中的 JPA 似乎没有返回任何结果的主要内容,如果未能解决你的问题,请参考以下文章

JPA 查询未在列表中返回更新的结果

java中当表中没有记录,为空的时候,用jpa查询的结果返回的是null吗?

java jpa此查询返回错误

Spring Boot、JPA 和 MySQL 返回空结果

Spring JPA Query by Example不返回任何结果

Spring JPA Repository findAll 在 JUnit 测试中不返回任何数据