【问题标题】:Custom Query with pagination not working in spring boot JPA repository带有分页的自定义查询在 Spring Boot JPA 存储库中不起作用
【发布时间】:2021-01-04 01:45:33
【问题描述】:

在 Spring JPA 中使用 Pagination 时,我需要根据某些条件获取所有记录。

我尝试了两种方法:

1.

@Query(value = "SELECT * FROM channel c, channel_plans cp, channel_pricing cpr where c.id=cp.channel_id AND cp.id=cpr.plans_id AND CURRENT_TIMESTAMP between cpr.validity_start and cpr.validity_end and cpr.is_active=1",nativeQuery = true)
Page<channel> findAll(Pageable pageable);
@Query(value = "SELECT * FROM channel c, channel_plans cp, channel_pricing cpr where c.id=cp.channel_id AND cp.id=cpr.plans_id AND CURRENT_TIMESTAMP between cpr.validity_start and cpr.validity_end and cpr.is_active=1",
    countQuery = "SELECT count(*) FROM channel c, channel_plans cp, channel_pricing cpr where c.id=cp.channel_id AND cp.id=cpr.plans_id AND CURRENT_TIMESTAMP between cpr.validity_start and cpr.validity_end and cpr.is_active=1",nativeQuery = true)
Page<channel> findAll(Pageable pageable);   

实体:

@Entity
public class ChannelPricing {
    @Id
    private int id; 
    
    @Column(name="validity_start") 
    private String validityStart; 

    @Column(name="validity_end")
    private String validityEnd; 

    @Column(name="is_active") 
    private int isActive;

    // other entity fields, getters, setters, equals, toString etc...
}
@Entity
    public class Channel {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        
        @OneToMany(cascade = CascadeType.ALL)
        @JoinColumn(name = "channel_id",referencedColumnName = "id")
        private List<ChannelPlans> plans;
        
        // other entity fields, getters, setters, equals, toString etc...
        }
@Entity
    public class ChannelPlans {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private int id;
    
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="plans_id",referencedColumnName ="id")
    private List<ChannelPricing> priceInfo;
    // other entity fields, getters, setters, equals, toString etc...
    }

我想获取所有处于活动状态的记录 (is_active=1)。但在这两种方法中,我都会收到活动和非活动记录。

这个查询似乎没有执行,能否请您帮我了解一下我是否遗漏了什么?

【问题讨论】:

  • 您应该提供额外的信息。您可以在问题中添加channel 实体吗?你得到了什么结果?你期待什么结果?
  • 我想获取所有活跃的记录(is_active=1)。但是我收到了活跃和非活跃的记录
  • 能否提供channel实体类?
  • 我觉得你需要 channel_pricing 来验证{private int id;私人双倍费率;私有 int 视图;私人int cpt;私人国际电视;私人int cpp;私有字符串免责声明;私人int点; @Column(name="spot_rate") 私人双 spotRate; @Column(name="spot_length") 私有 int spotLength ; @Column(name="validity_start") 私有字符串有效性开始; @Column(name="validity_end") 私有字符串有效性结束; @Column(name="rate_card") private int rateCard; @Column(name="is_active") private int isActive;}
  • 为了帮助解决问题,我们必须了解您的数据结构。请为所有相关实体提供必要的字段

标签: mysql sql spring spring-boot spring-data-jpa


【解决方案1】:

如果Channel 中的任何一个至少有一个有效价格,您的查询会将其作为结果返回。 Channel 实例将包含所有 ChannelPlans,其中包含所有 ChannelPricing(活动和非活动)。您无法在另一个实体中获取 ChannelPricing 列表的一部分。允许保持列表延迟加载或获取完整列表。

要激活ChannelPricing,只有查询应该返回Page&lt;ChannelPricing&gt;

实体:

@Entity
public class Channel {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
        
    @OneToMany(mappedBy = "channel", cascade = CascadeType.ALL)
    private List<ChannelPlans> plans;
        
    // other entity fields, getters, setters, equals, toString etc...
}

@Entity
public class ChannelPlans {    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private int id;

    @ManyToOne
    @JoinColumn(name = "channel_id")
    Channel channel;
    
    @OneToMany(mappedBy = "channelPlans", cascade = CascadeType.ALL)
    private List<ChannelPricing> priceInfo;
    // other entity fields, getters, setters, equals, toString etc...
}

@Entity
public class ChannelPricing {
    @Id
    private int id; 

    @ManyToOne
    @JoinColumn(name = "plans_id")
    ChannelPlans channelPlans;
    
    @Column(name="validity_start") 
    private LocalDateTime validityStart; 

    @Column(name="validity_end")
    private LocalDateTime validityEnd; 

    @Column(name="is_active") 
    private int isActive;

    // other entity fields, getters, setters, equals, toString etc...
}

存储库:

public interface ChannelPricingRepository extends JpaReposirory<ChannelPricing, Integer> {
    @Query("from ChannelPricing p where p.isActive=1 and p.validityStart<=now() and p.validityEnd>=now()")
    Page<ChannelPricing> getActivePrices(Pageable pageable);
}

【讨论】:

    猜你喜欢
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 2012-06-02
    • 2021-04-16
    • 2022-01-25
    • 2021-04-01
    • 2018-05-18
    相关资源
    最近更新 更多