开发者

Doubt regarding JPA namedquery

开发者 https://www.devze.com 2023-01-24 23:54 出处:网络
I am trying to execute a namedquery @NamedQuery(name=\"getEmployeeDetails\",query=\"select e.username,e.开发者_StackOverflow中文版email,e.image,e.firstname,e.lastname from Employee e where e.empid=?

I am trying to execute a namedquery

@NamedQuery(name="getEmployeeDetails",query="select e.username,e.开发者_StackOverflow中文版email,e.image,e.firstname,e.lastname from Employee e where e.empid=?1")

Now when I execute this query in a EJB 3.0 Session Bean what is the object I should return.I tried returning Listits returning a Vector which creates a classcast exception.The employee table contains fields like password and other confidential details which I don't want to fetch.So I am not using select e from Employee e. I am learning JPA can anyone help.


Below is the sample query which fetches only the required fields, but have to make such constructor for it.

Query : 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 Employee entity with selected fields & remaining will have default values.


Inspect the returned type by calling .getClass() on a returned object. I'd guess it's an array.

But this is not really a good way to use JPA. Select the whole entity and then just don't use what you don't need. It's not such a performance hit.

0

精彩评论

暂无评论...
验证码 换一张
取 消