java Spring JPA - 按 EmbeddedId 部分查找

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43865079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:46:49  来源:igfitidea点击:

Spring JPA - Find By EmbeddedId partially

javahibernatejpaspring-data-jpa

提问by Amit Phaltankar

Below code is for demo purpose only.

以下代码仅用于演示目的。

My Entitybean looks like this

我的实体bean 看起来像这样

@Entity
class Employee {

    @EmbeddedId
    private EmployeeKey employeeKey;

    private String firstName;
    private String lastName;

    // Other fields
    // Getter and Setters
}

The Embeddableclass:

嵌入式类:

@Embeddable
class EmployeeKey implements Serializable {

    private int employeeId;
    private String branchName;
    private String departmentName;

    //Getter and Setters
}


I can write JPARepositoryinterface method to find Employees by the EmbeddedId that returns me results as well.

我可以编写JPARepository接口方法,通过也返回结果的 EmbeddedId 来查找员工。

interface EmployeeRepository extends JpaRepository<Employee, EmployeeKey> {
        List<Employee> findByEmployeeKey(EmployeeKey employeeKey);
}


Question:Suppose, while querying I have employeeIdand branchNameonly, and I don't want to put filter on departmentName

问题:假设,在查询时我只有员工ID分支名称,我不想在部门名称上放置过滤器

  • In such cases how can I write my Repository method
  • Does JPA have something in-build for such scenario?
  • 在这种情况下,我该如何编写我的 Repository 方法
  • JPA 是否有针对这种情况的内置内容?

采纳答案by Amit Phaltankar

Here is how it worked for me.

这是它对我的工作方式。

@Ketrox's answer is absolutely correct and works fine. But in my real scenario I had 6 fields to search by and which resulted in an 120+ characters long method name. (Something like below)

@Ketrox 的回答绝对正确并且工作正常。但在我的真实场景中,我有 6 个字段可供搜索,这导致了 120 多个字符长的方法名称。(类似于下面的东西

List<Employee> findByEmployeeKeyField1AndEmployeeKeyField2AndEmployeeKeyField3AndEmployeeKeyField4AndEmployeeKeyField5AndEmployeeKeyField6(String field1, String field2, String field3, String field4, String field5, String field6);

Which is certainly not good enough to read and more than good enough to make codenarcunhappy.

这当然不足以阅读,而且足以让codenarc不高兴。



Finally I used find by example and that turned out to be really pleasant solution.

最后,我使用了 find by example,结果证明这是一个非常令人愉快的解决方案。

Repository:

存储库:

//skipped lines    
import org.springframework.data.domain.Example
//skipped lines
interface EmployeeRepository extends JpaRepository<Employee, EmployeeKey>{
    List<Employee> findAll(Example<Employee> employee);
}

Usage:

用法:

// Prepare Employee key with all available search by keys (6 in my case)
EmplyeeKey key = new EmplyeeKey();
key.setField1("field1_value");
key.setField2("field2_value");
//Setting remaining 4 fields

// Create new Employee ans set the search key
Employee employee = new Employee();
employee.setEmployeeKey(key);


// Call the findAll by passing an Example of above Employee object
List<Employee> result = employeeRepository.findAll(Example.of(employee));

I have elaborated the search by Spring Data JPA find by @EmbeddedId Partially

我已经通过 @EmbeddedId 部分详细说明了Spring Data JPA find 的搜索

回答by ketrox

List<Employee> findByEmployeeKeyEmployeeIdAndEmployeeKeyDepartmentName(int Id,String name);

Should work Have a look at query derivation

应该工作看看查询派生