Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA NamedQuery Result(returning partial list of Class fields)
JPA NamedQuery Result [message #503061] Thu, 10 December 2009 17:54 Go to next message
Larry  is currently offline Larry Friend
Messages: 30
Registered: December 2009
Member
Sorry if this is easy question, new to JPA!

I would like to create a named query that returns only a few fields of an Entity.

Something like this "select emp.name,emp.id from Employee emp".

I would like to get back an instance on Employee with just those fields filled in. Is this possible?? It seems pretty cumbersome to have to use "List<Object[String,int]>" .

Thanks for the help.
Re: JPA NamedQuery Result [message #503107 is a reply to message #503061] Thu, 10 December 2009 21:17 Go to previous message
Doug Clarke is currently offline Doug ClarkeFriend
Messages: 155
Registered: July 2009
Senior Member
You can use a FetchGroup to specify the attributes you want loaded. The entity will then load the rest of its state upon demand.

public List<Employee> fetchGroupExample(EntityManager em) {
Query query = em.createQuery("SELECT e FROM Employee e ORDER BY e.id");

FetchGroup fetchGroup = new FetchGroup();
fetchGroup.addAttribute("id");
fetchGroup.addAttribute("firstName");
fetchGroup.addAttribute("lastName");

query.setHint(QueryHints.FETCH_GROUP, fetchGroup);

return query.getResultList();
}


Doug
Previous Topic:Tomcat and eclipseLInk
Next Topic:Selecting data using EmbeddedID
Goto Forum:
  


Current Time: Sun Nov 23 02:16:38 GMT 2014

Powered by FUDForum. Page generated in 0.01876 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software