JPA queries are very easy if we are going to access through direct primary key . How can we retrieve the data based on the attributes in the nested objects. This JPA nested attributes are Foreign keys in these table. We can achieve this using Query attribute. Here is an example
Employee.java
{
String employeeName;
String location;
Department department
}
Deparment.java
{
String name;
String legalEntity;
String departmentId;
}
In Employee table department id is foreign key attribute.In the above JPA custom query we select data using reference attributes.
Employee.java
{
String employeeName;
String location;
Department department
}
Deparment.java
{
String name;
String legalEntity;
String departmentId;
}
public interface TestRepository extends JpaRepository<EmployeeEntity, Integer> { @Query("SELECT p FROM EmployeeEntity p WHERE p.department.departmentId = :departmentId") EmployeeEntity getEmployee(@Param("departmentId")String departmentId); }
In Employee table department id is foreign key attribute.In the above JPA custom query we select data using reference attributes.
0 Comments
Post a Comment