现在我正在统计类似这样的主题的浏览量:
这相当于SQL查询。
我可以使用
更新:以下是详细条目信息:
Long countByViewOnTopicId(Long topicId);
这相当于SQL查询。
select count(*) from views where topic_id = ?
这个查询可以给我所有浏览次数的数量,但我需要计算唯一用户的数量。我需要一个与下面查询等价的JPA查询:
select count(distinct user_id) from views where topic_id = ?
我可以使用
@Query
注解,但是我正在尝试在项目中减少自定义SQL的编写,像下面这样:Long countDictinctUserIdByViewOnTopicId(Long topicId);
更新:以下是详细条目信息:
@Entity
@Table(name = "views")
public class Views {
@Id
@GeneratedValue
private Long id;
@NotNull
@Column(name = "user_id", insertable = false, updatable = false)
private Long userId;
@JoinColumn(name = "user_id")
@ManyToOne
private User user;
@NotNull
@Column(name = "topic_id", insertable = false, updatable = false)
private Long topicId;
@JoinColumn(name = "topic_id")
@ManyToOne
private Topic topic;
@NotNull
@Column(name = "action_type")
@Enumerated(EnumType.ORDINAL)
private ActionTypes actionType;
Getter, Setter...
Long countDictinctUserIdByViewOnTopicId(Long topicId);
这个函数无法正常工作,这是否正确? - SynUser user
字段,请尝试使用countDictinctUser
而不是countDictinctUserId
。 - Vladimir VagaytsevcountDistinctUserIdByViewOnTopicId
来代替:-S - Aritzdistinct
的案例参考? - Aritz