Skip to content
Advertisement

JPA Entity for View containig joins and aliases

I have the following query using which I want to create an entity class for the columns which I am retrieving from the query.

select e.emp_id,
       c.company_code,
       mg.emp_id as mangaer_code
from employee e
         left join company c on e.emp_id = c.emp_id
         left join manager mg on e.emp_id = c.emp_id = mg.emp_id

How to create an entity class from these columns and what variables are needed to take it in entity class to refer to these columns?

Advertisement

Answer

View is a virtual table based on the result-set of an SQL statement or a function and JPA treats it as a regular table. Create a entity and use JPA annotations as below

@Entity
@Immutable
@Table(name = "employee_view")
 public class EmployeeView{

//define the required columns from view here

}

For more details, refer to this article that I found https://medium.com/@jonathan.turnock/exposing-subset-view-of-the-database-with-a-jpa-repository-over-rest-5b9d6e07344b

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement