Spring Data JPA JPQL列名在计数函数中被丢弃

时间:2018-10-11 06:41:35

标签: spring jpa jpql

我有一个使用Spring Data JPA JPQL选择一些记录的代码。

我有两个实体:

public class Cheat implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "cheat_seq", length = 10)
    private Long cheatSeq;

    @OneToMany(mappedBy = "cheat")
    private Set<CheatGoodVote> goodVote;

    // skipped..
}

public class CheatGoodVote {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="vote_seq", length=10)
    private Long voteSeq;

    @Column(name="ip_address", nullable=false)
    private String ipAddress;

    @Column(name="reg_date", nullable=false)
    private Date regDate;

    @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinColumn(name="cheat_fk", referencedColumnName="cheat_seq")
    public Cheat cheat;


}

我的存储库很简单:

public interface CheatRepository extends JpaRepository<Cheat, Long>{

    @Query("SELECT c FROM Cheat c WHERE COUNT(c.goodVote) <= :voteCnt")
    Page<Cheat> findByVoteLessThan(@Param("voteCnt") Long voteCnt, Pageable page);
}

当我调用方法 CheatRepository.findByVoteLessThan()时, 它在SQL下执行。

select 
cheat0_.cheat_seq as cheat_se1_0_, cheat0_.answer as answer2_0_, cheat0_.question as question3_0_, cheat0_.reg_date as reg_date4_0_, cheat0_.writer_ip as writer_i5_0_ 
from cheat cheat0_ cross join cheat_good_vote goodvote1_ 
where cheat0_.cheat_seq=goodvote1_.cheat_fk and count(.)<=? 
order by cheat0_.reg_date desc limit ?

但是在该SQL中,WHERE子句中有奇怪的代码 count(。)<=?。也许是引发错误的原因。

面对这个问题的原因可能是什么? 谢谢。

1 个答案:

答案 0 :(得分:0)

COUNT是一个聚合函数,不能在where子句中使用。相反,您可以使用SQL本机内部查询或Haven子句。