简体   繁体   English

JPA 将存储库 findBy 与实体对象一起使用

[英]JPA use repository findBy with an entity object

I'm trying to create a spring boot application, and I would like to set up a research feature.我正在尝试创建一个 Spring Boot 应用程序,并且我想设置一个研究功能。

What I've done so far :到目前为止我所做的:

I have two classes, Person.java and House.java with a OneToOne relationship between these two classes.我有两个类, Person.javaHouse.java ,这两个类之间存在OneToOne关系。 I'd like to create a research function which allows me to find a Person depending on the House properties.我想创建一个研究功能,它允许我根据房屋属性找到一个人。 For exemple find all persons who have houses with zipcode "45000" and city "ORLEANS".例如,查找所有拥有邮政编码为“45000”且城市为“ORLEANS”的房屋的人。 For this I've declared a function in my repository findByHouseZipcodeAndCity .为此,我在我的存储库findByHouseZipcodeAndCity声明了一个函数。

Now, the problem is that my House entity has about 15 properties, and I want to perform researches with any of these 15 properties : zipcode only, zipcode and city, city and surface... which makes a lot of combinations, and I don't want to create findBy methods for each combination.现在,问题是我的 House 实体有大约 15 个属性,我想对这 15 个属性中的任何一个进行研究:仅邮政编码、邮政编码和城市、城市和地表……这有很多组合,我不不想为每个组合创建findBy方法。

I've tried to give as a parameter to my findBy a House object which will contain the search criterias.我试图将包含搜索条件的 House 对象作为参数提供给我的findBy This is not working as hibernate throws me this error : object references an unsaved transient instance - save the transient instance before flushing .这不起作用,因为休眠向我抛出此错误: object references an unsaved transient instance - save the transient instance before flushing The problem is that I'm creating an instance of a House, I fill it with my search criterias, and I use it for my findBy, but I found the findBy makes a flush() , while the House entity is not saved (And I don't want it to be saved since it's only for a search).问题是我正在创建一个 House 的实例,我用我的搜索条件填充它,并将它用于我的 findBy,但我发现 findBy 生成了一个flush() ,而 House 实体没有被保存(并且我不想保存它,因为它仅用于搜索)。

I've found solutions like using JPA Criterias, but I'd like to know if I can perform my search just by using an entity as a parameter for a findBy method.我找到了使用 JPA Criterias 之类的解决方案,但我想知道是否可以仅通过使用实体作为 findBy 方法的参数来执行搜索。

Thank you in advance for your help预先感谢您的帮助

PersonRepository.java PersonRepository.java

@Repository
public interface PersonRepository extends JpaRepository<Person, Long>{
    List<Person> findByHouseZipcodeAndHouseCity(String zipcode, String city);
    List<Person> findByHouse(House house);
}

Person.java人.java

@Entity
public class Person{

    @Id
    @SequenceGenerator(name = "INFO_ACTION_SEQ", sequenceName = "INFO_ACTION_SEQ")
    @GeneratedValue(generator = "INFO_ACTION_SEQ", strategy = GenerationType.AUTO)
    @Column(name = "ID_PERSON", columnDefinition = "INTEGER")
    private Integer id;

    private String name;

    private String surname;

    @OneToOne(cascade = CascadeType.ALL)
    private House house;

}

House.java房子.java

@Entity
public class House{

    @Id
    @SequenceGenerator(name = "INFO_ACTION_SEQ", sequenceName = "INFO_ACTION_SEQ")
    @GeneratedValue(generator = "INFO_ACTION_SEQ", strategy = GenerationType.AUTO)
    @Column(name = "ID_HOUSE", columnDefinition = "INTEGER")

    private Integer id;

    private String city;
    private String street:
    private String zipcode;
    private String country;
    // Other properties...
}

You could use query by example , a user-friendly querying technique that allows dynamic query creation and does not require you to write queries at all.您可以使用query by example ,这是一种用户友好的查询技术,它允许动态创建查询并且根本不需要您编写查询。

Consider the following entity:考虑以下实体:

@Data
@Entity
public class Person {

    @Id
    private String id;
    private String firstname;
    private String lastname;
    private Address address;
}

To search all persons with a given name, you can use:要搜索具有给定姓名的所有人员,您可以使用:

Person person = new Person();
person.setFirstname("Dave");

Example<Person> example = Example.of(person);
Iterable<Person> searchResult = repository.findAll(example);

And, if you need to search for a single person, use:而且,如果您需要搜索一个人,请使用:

Person searchResult = repository.findOne(example);

列表中的 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 如何在具有复合PK的实体中使用“findBy”(Hibernate JPA) - How to use “findBy” in an Entity with a Composite PK (Hibernate JPA) JPA存储库findBy具有多个或运算符的方法签名? - Jpa repository findBy method signature with multiple or operators? Spring 引导应用程序中的 Jpa 存储库按问题查找 - Jpa Repository in Spring boot app findBy issue JPA 每次findBy新建object - JPA each time findBy create new object 将 GROUP BY 和 ORDER BY 与 object 上的 JPA 存储库一起使用 - use GROUP BY and ORDER BY with JPA repository on object 如何在 spring 引导中由 @OneToMany 注释映射的列上使用 JPA findBy - How to use JPA findBy on column mapped by @OneToMany annotation in spring boot 如何在@OneToMany 关系映射的列上使用 JPA findBy 查询? - How to use JPA findBy query on column mapped by @OneToMany relationship? 使用 spring 的 Redis 存储库中的 FindBy - FindBy in Redis Repository with spring Spring JPA存储库findBy直接通过外键使用POJO代替类的数据成员 - Spring JPA Repository findBy foreign key directly with POJO instead of data member of class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM