Spring Data findTop10- Ten requests instead of one

I have a Spring Data Method:

List<RegionBasics> findTop10ByRegionMappingsActiveTrue();

      

I expect it to find the top 10 records from the db in a single query, but what I see in the logs (I didn't insert whole logs to keep this reading, but this select query gets called 10 times):

select regionmapp0_.id as id1_2_1_, region_basi_.is_active, as is_activ2_2_1_, regionmapp0_.region_basic_id as region_b3_2_1_, regionbasi1_.id as id1_1_0_, region_basi1_.hotel_name1 as hotel_co2 .type type5_1_0_ from region_mappings regionmapp0_ left external join region_basics regionbasi1_ on regionmapp0_.region_basic_id = regionbasi1_.id where regionmapp0_.region_basic_id =?

How can I ensure that this method only hits the db once (instead of 10)?

My model:

@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;
}

      

+3


source to share


1 answer


I think you should join to fetch RegionMappings: For example: @Query("SELECT rb FROM RegionBasics r JOIN FETCH r.regionMappings rm WHERE rm.active=true")

With page parameternew PageRequest(0,10)



0


source







All Articles