简体   繁体   English

Spring 数据JPA:如何形成条件查询?

[英]Spring Data JPA: How to form a conditional query?

Consider a table with 4 fields: id, name, age, date.考虑一个包含 4 个字段的表:id、name、age、date。 Now I want to look up the Database using these fields.现在我想使用这些字段查找数据库。 So that's simple, I can do:所以这很简单,我可以这样做:

@Query("select d from data d where d.name=:name and d.age=:age and d.date=:date")

But the problem is, name, age and date are optional fields, ie age, name, the date can be null. If age is null, then I should look up the Database using name and date and if age and name are NULL, then I should look up the Database using the date alone and so on...但问题是,name, age and date 是可选字段,即age, name, date 可以是null。如果age 是null,那么我应该使用name 和date 查找数据库,如果age 和name 是NULL,那么我应该单独使用日期查找数据库等等......

Is it possible to achieve this in a simple way rather than forming different queries for each scenario?是否有可能以一种简单的方式实现这一点,而不是为每个场景形成不同的查询? Because the real scenario that I am working on has 6-7 optional fields and forming different queries for each one looks damn weird.因为我正在处理的真实场景有 6-7 个可选字段,并且为每个字段形成不同的查询看起来很奇怪。

For optional field, use this: @Query("select d from data d where (:name is null or d.name=:name) and (:age is null or d.age=:age) and (:date is null or d.date=:date)")对于可选字段,使用:@Query("select d from data d where (:name is null or d.name=:name) and (:age is null or d.age=:age) and (:date is null或 d.date=:date)")

This is a very common requirement where we want a hibernate query result based on search parameters or custom conditions.这是一个非常常见的需求,我们希望根据搜索参数或自定义条件获得 hibernate 的查询结果。

After the introduction of JPA, it is hard to write such a query.引入JPA后,这样的查询就很难写了。

But there is a way by which you can create custom queries based on conditions.但是有一种方法可以让您根据条件创建自定义查询。

Use EntityManager which will help you to execute HQL and Native query and your further implementation will remain the same.使用EntityManager这将帮助您执行 HQL 和本机查询,您的进一步实现将保持不变。 Declare EntityManager object in your implementation and then use its methods to get data based on a query,在您的实现中声明 EntityManager object,然后使用其方法根据查询获取数据,

@PersistenceContext
EntityManager entityManager;

Execute your query:执行您的查询:

entityManager
    .createQuery(queryString);

An important point: If you have a requirement for pagination then It will not give you the total record count which you can easily get in a JPA Page object by calling its getTotalElements() method.重要的一点:如果您有分页要求,那么它不会给您总记录数,您可以通过调用其 getTotalElements() 方法在 JPA 页 object 中轻松获得。 In this implementation, if you need the total count also then you have to call a separate query for the count.在此实现中,如果您还需要总计数,则必须为计数调用单独的查询。

Review detail example: Add Custom Functionality to a Spring Data Repository查看详细示例: 将自定义功能添加到 Spring 数据存储库

Check this one out:- Spring Data JPA and Null Parameters...检查这一点:- Spring 数据 JPA 和 Null 参数...

https://www.baeldung.com/spring-data-jpa-null-parameters https://www.baeldung.com/spring-data-jpa-null-parameters

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM