Spring Boot + JPA:如何使这个find方法起作用?

huangapple go评论73阅读模式
英文:

Spring Boot + Jpa : How to make this find method work?

问题

我遇到了一个问题,但是我找不到一个确切的答案。我有一个名为 "Post" 的实体和一个名为 "Tag" 的实体,如下所示。由于篇幅原因,我只添加了必要的代码。我还在使用MySQL作为数据库。

@Entity
public class Post {

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "POST_TAG", joinColumns = {@JoinColumn(name = "post_id")},
            inverseJoinColumns = {@JoinColumn(name = "tag_id")})
    private Set<Tag> tags = new HashSet<>();
}
@Entity
public class Tag {
    @ManyToMany(mappedBy = "tags")
    private Set<Post> posts = new HashSet<>();
}

我尝试的目标是,在数据库中找到具有特定标签的所有文章,并且我想通过名称输入该标签。为了实现这一点,我尝试了不同的JPA方法,最后一个是这个:

List<Post> findAllByTagByNameOrderByCreatedAt(String name);

问题是,无论我选择什么单词组合,我总是会收到类似于以下错误的错误信息:

IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.blogApp2.repository.PostRepository.findAllByTagByNameOrderByCreatedAt(java.lang.String)! No property tagByName found for type Post!

正如你所见,在这两个实体之间存在多对多的关系,所以在数据库中有一个中间表连接它们。因此,这两个实体在该中间表中相互引用。因此,在"Post"表中没有标签的名称。

我该如何创建一个方法,通过输入特定的标签名称来查找所有文章?

英文:

I have an issue for which I couldn't find a concludent answer. I am having Post entity and a Tag entity as you can see below. I have added only the necessary code for space reasons. I am also using MySQL as database.

@Entity
public class Post {

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = &quot;POST_TAG&quot;, joinColumns = {@JoinColumn(name = &quot;post_id&quot;)},
            inverseJoinColumns = {@JoinColumn(name = &quot;tag_id&quot;)})
    private Set&lt;Tag&gt; tags = new HashSet&lt;&gt;();
@Entity
public class Tag {
    @ManyToMany(mappedBy = &quot;tags&quot;)
    private Set&lt;Post&gt; posts = new HashSet&lt;&gt;();

What I am trying to do is find a All the post in the database that have a certain Tag, and I want to input that tag by name. In order to do that I have tried different jpa methods, the last one being this one:

 List&lt;Post&gt; findAllByTagByNameOrderByCreatedAt(String name);

The problem is that I am always getting an error like this no matter what combination of words I choose

IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.blogApp2.repository.PostRepository.findAllByTagByNameOrderByCreatedAt(java.lang.String)! No property tagByName found for type Post!

As you can see there is a manytomany relationship between the 2 entities so I have a middle table between them in the database. So the 2 entities have their references to each other in that middle table. So I have no tags name in the Post table.

How can I create a method to find all the posts by inputting a certain tag name?

答案1

得分: 1

很明显,错误提示指出类型“Post”没有“tagByName”的方法。
您可以使用Query注解并编写SQL查询来连接表,并按标签名称查找。

英文:

It is clear that the error says there is no tagByName for type Post.
You can use Query annotation and write an sql query to join the tables and find by the tag name.

答案2

得分: 1

实际上,不需要自定义查询,只需要像属性名一样明确指定,所以您也需要使用复数形式。

另外,我假设您的"Tag"实体中有一个属性名。

它会类似于这样:

 List<Post> findAllByTagsNameOrderByCreatedAt(String name);
英文:

Actually, there is no need for custom query, it just needs to be specified exactly like your property name, so you need to use plural as well.

also, I asume, you have a property name in your Tag Entity.

It will look something like this:

 List&lt;Post&gt; findAllByTagsNameOrderByCreatedAt(String name);

huangapple
  • 本文由 发表于 2020年9月22日 00:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63996331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定