别忘了检查下面的图片
物体:
1) 问题
2) 标签
这种关系有很多种
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "question_has_tag",
joinColumns = @JoinColumn(name = "question_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id"))
private List<Tag> tags;
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Column
private String name;
@ManyToMany(mappedBy = "tags", fetch = FetchType.LAZY)
@ContainedIn
private List<Question> questions;
mysql查询如下:
select t.id, t.name, count(question_has_tag.tag_id) as i from tag as t join question_has_tag
on id = question_has_tag.tag_id group by id order by i desc;
需要翻译成jpql或hql
主要目标是获得最频繁的标签列表like:enter image 此处为说明
1条答案
按热度按时间nkkqxpd91#
尝试:
给定的jpa查询(通过hibernate)转换为sql: