Doubt about named JPA requirement

I am trying to execute namedquery

@NamedQuery(name="getEmployeeDetails",query="select e.username,e.email,e.image,e.firstname,e.lastname from Employee e where e.empid=?1") 

Now, when I execute this request in an EJB 3.0 Bean session, what the object should be, I should return. I tried to return a list returning a vector that throws a class exception. The employee table contains fields such as password and other sensitive data that I do not want to receive. So I do not use select e from Employee e . I am learning JPA, can anyone help.

0
source share
2 answers

Below is an example of a query that retrieves only the required fields, but should make such a constructor for it.

Request: SELECT NEW package_name.Employee(e.username,e.email,e.image,e.firstname,e.lastname) FROM Employee e where e.empid=?1;

It will return an Employee object with the selected fields, and the rest will have default values.

+6
source

Inspect the return type by calling .getClass() on the return object. I would suggest that this is an array.

But this is not a good way to use JPA. Select the whole object, and then just don’t use what you don’t need. This is not such a performance.

+5
source

Source: https://habr.com/ru/post/1334343/


All Articles