0

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?

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
  • install jpa buddy plugin for intelij it will help you in creating entity models, you need to improve the question with the relations between tables are they one to one, one to many, may to many ? I guess you need 3 entity classes for each table – xMilos Dec 16 '21 at 13:06
  • 1
    as you didn't even provide the type of the columns, noone can really help you with this, please provide more information – PaulD Dec 16 '21 at 13:17
  • Do you need an entity class for this - Its not like you can perform writes on the data through the entity, so don't really need it managed. Look at constructor queries in JPA - you can have any java object built from the data and returned from the query without having to have entity mappings on it - it just needs a constructor that takes in the values. – Chris Dec 16 '21 at 15:49
  • You need to provide more information on how you are running this query. It looks like native SQL? And you are running this with Hibernate/JPA? If yes maybe you can look at these answers: [ONE](https://stackoverflow.com/questions/17708946/jpa-native-query-select-and-cast-object) [TWO](https://stackoverflow.com/questions/13012584/jpa-how-to-convert-a-native-query-result-set-to-pojo-class-collection?rq=1) – Philipp Dec 16 '21 at 16:56

1 Answers1

0

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