【发布时间】:2021-01-15 07:45:47
【问题描述】:
我想为博客创建一个简单的“您可能也喜欢”功能。 有帖子,每个帖子都有一个或多个标签。一个标签也可以包含许多帖子。我想实现你打开一个帖子的功能,并且将具有相似标签的帖子推荐给你。
所以我创建了 3 个实体:
Post.java
@Entity
@Table
public class Post {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String text;
@Column
private String author;
@OneToMany(mappedBy = "post")
Set<PostTags> postTags;
public Post(){}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Set<PostTags> getPostTags() {
return postTags;
}
public void setPostTags(Set<PostTags> postTags) {
this.postTags = postTags;
}
}
标签.java
@Entity
@Table
public class Tags {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Long id;
@Column
private String name;
@OneToMany(mappedBy = "tag")
Set<PostTags> postTags;
public Set<PostTags> getPostTags() {
return postTags;
}
public void setPostTags(Set<PostTags> postTags) {
this.postTags = postTags;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
PostTags.java
@Entity
public class PostTags {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column
private Long id;
@ManyToOne
@JoinColumn(name = "post_id")
private Post post;
@ManyToOne
@JoinColumn(name = "tag_id")
private Tags tag;
public PostTags(){}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
public Tags getTag() {
return tag;
}
public void setTag(Tags tag) {
this.tag = tag;
}
}
和存储库:
@Repository
public interface PostTagsRepository extends JpaRepository<PostTags, Long> {
@Query("select p.post from PostTags p where p.tag.id IN :tagIds")
Set<Post> findPostsbyTagIds (List<Long> tagIds);
}
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
@Query("select p from Post p where p.author = :author")
Set<Post> findPostsByAuthor(String author);
}
我设法以业余的方式创建了此功能,但总比没有好。我把所有东西都用一种方法堆积起来只是为了测试它:
@GetMapping("/posts")
public Set<Post> showRecommendedPosts(){
//Imitate post id
long postId = 1;
Post postFound = postRepository.findById(postId).get();
Set<PostTags> postTags = postFound.getPostTags();
List<Long> listTagIds = new ArrayList<>();
//extract ids of the tags from the post
for(PostTags tag : postTags){
listTagIds.add(tag.getTag().getId());
}
//find posts by Author
Set<Post> postsByAuthor = postRepository.findPostsByAuthor(postFound.getAuthor());
//find posts by Tags
Set<Post> postsByTagIds = postTagsRepository.findPostsbyTagIds(listTagIds);
//We combine both sets
Set<Post> recommendedPosts = new HashSet<>(postsByAuthor);
recommendedPosts.addAll(postsByTagIds);
recommendedPosts.remove(postFound);
return recommendedPosts;
}
但这只有在我像这样手动将数据添加到数据库中的“post_tags”表时才有效:
这是我的问题,我不知道如何在 Spring 中为帖子添加多个标签。因为它会是这样的:
PostTags newPostTag1 = new PostTags();
newPostTag.setPost(post1);
newPostTag.setTag(tag1);
PostTags newPostTag2 = new PostTags();
newPostTag2.setPost(post1);
newPostTag2.setTag(tag2);
PostTags newPostTag3 = new PostTags();
newPostTag3.setPost(post1);
newPostTag3.setTag(tag3);
等等…… 因此,这不是一个选择。那么如何正确保存标签?还是我的实体创建不正确?我的错误是什么?谢谢!
【问题讨论】:
标签: spring-boot hibernate jpa