Doubt regarding JPA namedquery

穿精又带淫゛_ 提交于 2019-11-28 11:45:21

问题


I am trying to execute a 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 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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4186828/doubt-regarding-jpa-namedquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!