openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pinaki Poddar <>
Subject Re: How to get selective columns only
Date Tue, 22 Dec 2009 17:16:35 GMT

  I tried your example as it is and OpenJPA worked as intended by generating
the following correct SQL query under the specified FetchPlan

    SELECT, t0.designation, FROM Employee t0 WHERE ( = ?)
[params=(long) 42]

  Your example does not reveal *how* you are verifying which states are
loaded into the selected Employee instances. One of the common mistakes is
to do something like
to verify whether DOJ field is loaded into emp instance. The fallacy is the
getDOJ() will load the value of DOJ field if it is not loaded and if emp is
still in a managed state. So correct way is to verify the field value by
  Of course, you can also intercept the SQL generated by OpenJPA to
ascertain what states are loaded by a specific FetchPlan. 

  I ran these tests on OpenJPA trunk -- I do not know whether the reported
error existed in an earlier 1.0.2 version as you seem to be using. 

 As a side note: 
   the example shows a named query parameter is used in JPQL but uses
positional index to set its value.
    "select o from employee o where id =:a"
     jpaquery.setParameter(1, empId);

   This is not kosher, though OpenJPA was lenient on such cases in early
versions, it is no more.  

Himadri wrote:
> I tried with the FetchGroup & FetchPlan but it didn't work.  I have an
> Entity called Employee(name, DOJ, designation) out of which I want to
> select only (name, designation) [DOJ shouldn't get populated in the
> resultList objects] 
> Attaching my code in Session Bean for your reference. 
> 	public Employee getEmployeeDetails(int empId)
> 	{
> 		OpenJPAQuery jpaquery =
> OpenJPAPersistence.cast(manager.createQuery("select o from employee o
> where id =:a"));
> 		FetchPlan fetchPlan = jpaquery.getFetchPlan();
> 		fetchPlan.clearFetchGroups();
>                           fetchPlan.clearFields(); 
> fetchPlan.removeFetchGroup(org.apache.openjpa.meta.FetchGroup.NAME_DEFAULT); 	
> 		fetchPlan.addFetchGroup("empDetails");
> 		jpaquery.setParameter(1, empId);
> 		return (Employee)jpaquery.getResultList().get(0);
> 	} 
> FetchGroup "empDetails" is defined in Entity class Employee.
> @FetchGroup(name="empDetails",
> attributes={@FetchAttribute(name="designation"),
> @FetchAttribute(name="name")})
> Let me know in case you need anything for this.
> I configured FetchGroup from the reference of link given below.
> Pinaki Poddar wrote:
>> Hi,
>>   FetchPlan is the right solution fof your requirement. FetchPlan should
>> work for this case and has been used by many users so far for more
>> complex fetch requirements.
>>   May be there are some slight mistakes in how the fetch plans are
>> configured/used?
>> Himadri wrote:
>>> Hi ,
>>> I have a requirement to get only selective colums from an Entity and all
>>> I want something like 
>>> Select , o.dec from Employee o 
>>> instead of 
>>> Select 0 from Employee o.
>>> How can I achive this using openJpa  ? I though FetchPlan is the answer
>>> , where in FetchAttributes will have the columns to be fetched , but it
>>> doesnt work. OpenJPA gets all the attributes of the columns not the only
>>> selected one?

View this message in context:
Sent from the OpenJPA Users mailing list archive at

View raw message