我有一个代码,通过使用选择一些记录。
我有两个实体:
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条款。也许这就是抛出错误的原因。
面对这个问题的原因是什么?谢谢。
发布于 2018-10-11 06:55:04
COUNT是不能在where子句中使用的聚合函数。相反,您可以使用SQL本机内部查询或having子句。
https://stackoverflow.com/questions/52753816
复制相似问题