[英]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.java和House.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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.