简体   繁体   English

如何使用包含关键字的属性创建 Spring JPA 存储库 findBy 查询?

[英]How do you create a Spring JPA repository findBy query using a property that contains a keyword?

Here is a simplified example of my problem.这是我的问题的简化示例。 I have this repository and entity class.我有这个存储库和实体类。

public interface ThingRepository extends JpaRepository<ThingEntity, Long> {
    ThingEntity findByFooInAndBar(String fooIn, String bar);
}

@Entity
public class ThingEntity {
    @Column(name="FOO_IN", nullable=false, length=1)
    private String fooIn;

    public String getFooIn() {
        return fooIn;
    }

    public setFooIn(String fooIn) {
        this.fooIn = fooIn;
    }

    /* not including bar property for brevity's sake */
}

Spring is throwing the following exception. Spring 抛出以下异常。

org.springframework.data.mapping.PropertyReferenceException: No property foo found for type ThingEntity!

It looks like Spring is taking the method findByFooInAndBar and thinks that foo is my property name and in is a keyword for matching values within a collection.看起来 Spring 正在采用findByFooInAndBar方法并认为foo是我的属性名称,而in是用于匹配集合中值的关键字。

How do I get it to understand that the property name is fooIn , not foo ?我如何理解属性名称是fooIn ,而不是foo

To overcome this problem, I've defined the query manually using the @Query annotation.为了解决这个问题,我使用@Query注释手动定义了查询。 I'll happily accept anyone else's answer if they find a solution that doesn't require a manual query.如果他们找到不需要手动查询的解决方案,我会很乐意接受其他人的回答。

public interface ThingRepository extends JpaRepository<ThingEntity, Long> {

    @Query("SELECT t FROM Thing t WHERE t.fooIn = ?1 AND t.bar = ?2")
    ThingEntity findByFooInAndBar(String fooIn, String bar);
}

Spring is parsing 'In' in your method to create the query. Spring 正在您的方法中解析“In”以创建查询。 Check the link for create your query: you should change the name of the variable fooIn to fooin or something like that...检查创建查询的链接:您应该将变量fooIn的名称更改为fooin或类似的名称...

I observed the code kindly and notice your return is not a list but an object .我友好地观察了代码,并注意到您的返回不是一个list而是一个object
Change that part to List<ThingEntity> .将该部分更改为List<ThingEntity>

列表中的 findBy 属性<object> SpringBoot JPA 存储库<div id="text_translate"><p>我的数据库 Story 和 Tag 中的两个对象之间存在一对多关系。</p><p> 我希望能够获取所有带有标签 object 和字符串名称的 Story 对象。</p><p> 故事.java</p><pre> @Entity @Table(name = "stories") public class Story { @Id @GeneratedValue private Long id; @Column(name = "title") private String title; @JsonIgnoreProperties({"story"}) @OneToMany(mappedBy = "story", fetch = FetchType.LAZY) private List&lt;Tag&gt; tags; public Story(String title){ this.title = title; } public Story(){ } // getters &amp; setters }</pre><p> 标签.java</p><pre> @Entity @Table(name = "tags") public class Tag { @Id @GeneratedValue private Long id; @Column(name = "name") private String name; @JsonIgnoreProperties({"tags"}) @ManyToOne @JoinColumn(name = "story_id", nullable = false) @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE) private Story story; public Tag(String name, Story story){ this.name = name; this.story = story; } public Tag(){ } /// getters &amp; setters }</pre><p> StoryController.java</p><pre> @RestController public class StoryController { @Autowired StoryRepository storyRepository; @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value = "/stories") public ResponseEntity&lt;List&lt;Story&gt;&gt; getAllStories(){ return new ResponseEntity&lt;&gt;(storyRepository.findAll(), HttpStatus.OK); } @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value="/stories/tagSearch/{name}") public ResponseEntity&lt;Story&gt; getStoryByTag(@PathVariable String name) { return new ResponseEntity(storyRepository.findByTags_Name(name), HttpStatus.OK); } @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value="/stories/{id}") public ResponseEntity&lt;Story&gt; getStory(@PathVariable Long id) { return new ResponseEntity(storyRepository.findById(id), HttpStatus.OK); } }</pre><p> StoryRepository.java</p><pre> @Repository public interface StoryRepository extends JpaRepository&lt;Story, Long&gt; { public List&lt;Story&gt; findByTags_Name(String name); }</pre><p> 尝试通过浏览器查询时,转到地址 localhost:8080/api/stories/tagSearch/?name="tag" 数据库返回数据库中的所有对象,而不是我要查找的结果。</p></div></object> - findBy property in List<Object> SpringBoot JPA Repository

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 列表中的 findBy 属性<object> SpringBoot JPA 存储库<div id="text_translate"><p>我的数据库 Story 和 Tag 中的两个对象之间存在一对多关系。</p><p> 我希望能够获取所有带有标签 object 和字符串名称的 Story 对象。</p><p> 故事.java</p><pre> @Entity @Table(name = "stories") public class Story { @Id @GeneratedValue private Long id; @Column(name = "title") private String title; @JsonIgnoreProperties({"story"}) @OneToMany(mappedBy = "story", fetch = FetchType.LAZY) private List&lt;Tag&gt; tags; public Story(String title){ this.title = title; } public Story(){ } // getters &amp; setters }</pre><p> 标签.java</p><pre> @Entity @Table(name = "tags") public class Tag { @Id @GeneratedValue private Long id; @Column(name = "name") private String name; @JsonIgnoreProperties({"tags"}) @ManyToOne @JoinColumn(name = "story_id", nullable = false) @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE) private Story story; public Tag(String name, Story story){ this.name = name; this.story = story; } public Tag(){ } /// getters &amp; setters }</pre><p> StoryController.java</p><pre> @RestController public class StoryController { @Autowired StoryRepository storyRepository; @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value = "/stories") public ResponseEntity&lt;List&lt;Story&gt;&gt; getAllStories(){ return new ResponseEntity&lt;&gt;(storyRepository.findAll(), HttpStatus.OK); } @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value="/stories/tagSearch/{name}") public ResponseEntity&lt;Story&gt; getStoryByTag(@PathVariable String name) { return new ResponseEntity(storyRepository.findByTags_Name(name), HttpStatus.OK); } @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value="/stories/{id}") public ResponseEntity&lt;Story&gt; getStory(@PathVariable Long id) { return new ResponseEntity(storyRepository.findById(id), HttpStatus.OK); } }</pre><p> StoryRepository.java</p><pre> @Repository public interface StoryRepository extends JpaRepository&lt;Story, Long&gt; { public List&lt;Story&gt; findByTags_Name(String name); }</pre><p> 尝试通过浏览器查询时,转到地址 localhost:8080/api/stories/tagSearch/?name="tag" 数据库返回数据库中的所有对象,而不是我要查找的结果。</p></div></object> - findBy property in List<Object> SpringBoot JPA Repository Spring 引导应用程序中的 Jpa 存储库按问题查找 - Jpa Repository in Spring boot app findBy issue 如何使用仅包含 1 个参数的多个包含创建 findBy 查询 - How to create findBy query with multiple contains with just 1 argument 如何使用findBy Spring存储库在Java中的Map中检索数据 - How to retrieve data within a Map in java using findBy spring repository 使用 spring 的 Redis 存储库中的 FindBy - FindBy in Redis Repository with spring 此时如何在Query上使用? -JPA存储库春季启动 - How to use at Query at this? - JPA repository spring boot 如何使用 ON 子句创建 Spring 数据 jpa 存储库 - how to create a Spring data jpa repository with a clause ON 如何为 spring 数据 jpa 存储库创建 bean? - How to create bean for spring data jpa repository? 我该如何使用属性而不直接在实体休眠Spring JPA上进行findBy操作 - How can I do findBy operations with attributes not directly on the entity hibernate spring JPA 无法创建查询元模型Spring Data JPA存储库(奇怪的行为) - Could not create query metamodel Spring Data JPA repository (Weird behaviour)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM