gpt4 book ai didi

java - Spring Data findTop10 - 十个查询而不是一个

转载 作者:行者123 更新时间:2023-12-02 06:09:38 26 4
gpt4 key购买 nike

我有这样的 Spring Data 方法:

List<RegionBasics> findTop10ByRegionMappingsActiveTrue();

我期望它会在一个查询中找到数据库中的前 10 条记录,但我在日志中看到的是(我没有粘贴整个日志以保持可读性,但此选择查询被调用 10 次):

select regionmapp0_.id as id1_2_1_, regionmapp0_.is_active as is_activ2_2_1_, regionmapp0_.region_basic_id as region_b3_2_1_, regionbasi1_.id as id1_1_0_, regionbasi1_.hotel_count as hotel_co2_1_0_, regionbasi1_.name_long as name_lon3_1_0_, regionbasi1_.name as name4_1_0_, regionbasi1_.type as type5_1_0_ from region_mappings regionmapp0_ left outer join region_basics regionbasi1_ on regionmapp0_.region_basic_id=regionbasi1_.id where regionmapp0_.region_basic_id=?

如何确保此方法仅命中数据库一次(而不是 10 次)?

我的模型:

@Data
@NoArgsConstructor
@Entity
@Table(name = "region_basics")
public class RegionBasics {

@Id
Integer id;

@Column
String type;

@Column
String name;

@Column(name = "name_long")
String longName;

@Column(name = "hotel_count")
Integer hotelCount;

@OneToOne(mappedBy="regionBasics")
RegionMappings regionMappings;
}

最佳答案

我认为你应该加入获取 RegionMappings:像这样:@Query("SELECT rb FROM RegionBasics r JOIN FETCH r.regionMappings rm WHERE rm.active=true")带可分页参数new PageRequest(0,10)

关于java - Spring Data findTop10 - 十个查询而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44494933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com