【发布时间】: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