Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JPA - Find By EmbeddedId partially

Below code is for demo purpose only.

My Entity bean looks like this

@Entity class Employee {      @EmbeddedId     private EmployeeKey employeeKey;      private String firstName;     private String lastName;      // Other fields     // Getter and Setters } 

The Embeddable class:

@Embeddable class EmployeeKey implements Serializable {      private int employeeId;     private String branchName;     private String departmentName;      //Getter and Setters } 

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

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

Question: Suppose, while querying I have employeeId and branchName only, and I don't want to put filter on departmentName

  • In such cases how can I write my Repository method
  • Does JPA have something in-build for such scenario?
like image 270
Amit Phaltankar Avatar asked May 09 '17 08:05

Amit Phaltankar


People also ask

How does JPA interpret partial primary key query in spring?

JPA Repository and Method Naming Let us quickly define our JPA repository interface by extending the JpaRepository with entity Book as well as BookId: We use a part of the id variable's field names to derive our Spring Data query methods. Hence, JPA interprets the partial primary key query as: 4. Conclusion

How to learn using JPA with spring and Spring Boot?

Want to learn using Java Persistence API (JPA) with Spring and Spring Boot ? As a practice each row in a database table should be uniquely identified by a key column. This column can be a data column which has unique values or a specifically created column of sequential numbers or random ids like UUID.

What is songid in JPA with spring and Spring Boot?

Which represents three columns Composite key. Want to learn using Java Persistence API (JPA) with Spring and Spring Boot ? The Song is a JPA based Entity object which has fields related to Songs along with a reference to the SongId instance. The SongId instance reference here is marked as @EmebeddedId .

Does spring data&JPA support composite primary keys?

The Spring Data & JPA does support composite primary keys and today we will see how. For this tutorial we will consider a table that stores Songs details such as name, genre, artist, rating, download link etc. We will create a Composite Primary Key of name, genre, and artist.


2 Answers

List<Employee> findByEmployeeKeyEmployeeIdAndEmployeeKeyBranchName(         int employId,         String branchName); 

Should work Have a look at query derivation

like image 78
ketrox Avatar answered Sep 28 '22 17:09

ketrox


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)

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 codenarc unhappy.


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

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

like image 20
Amit Phaltankar Avatar answered Sep 28 '22 19:09

Amit Phaltankar