Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-dev] Single Location for the Named Queries

Hi Eclipse Link Community,
 
    I think many people might have felt that annotating the named queries on entites is difficult to maintain over time and some times confusing in a way that finding a proper entity to place the query will be difficult in case of queries that fetch details from several entities.
 
There is already enough frustration documented about this on the bloggosphere, for example:
here and here.
 
How about a cental place like a constants file where we can put all related named queries in one class, like the one below?
 
@QueryCollection(
 
   @NamedQuery(name=NamedQueries.fetchAnnualSalaryOfAnEmployee,
                          query="SELECT EMP.id, EMP.name,
                          EMP.CTC FROM Employee EMP WHERE EMP.name=:employeeName"),
   @NamedQuery(name=Named.Queries.fetchEmployeeHierarchy,
                          query="SELECT Emp.id, Emp.name,
                           Mgr.id,Mgr.name FROM Employee Emp JOIN
                           Employee.manager Mgr  JOIN Emp.department Dept WHERE
                           Dept.name=:departmentName")
 
)
public class NamedQueries {
 
        private static final String fetchAnnualSalaryOfAnEmployee="fetchAnnualSalaryOfAnEmployee";
 
        private static final String fetchEmployeeHierarchy="fetchEmployeeHierarchy";
 
}
 
And then we can refer to these while executing the queries from the same constants file like:
 
               Query query=em.createNamedQuery(NamedQueris.fetchAnnualSalaryOfAnEmployee);
               query.getSingleResult();
 
 
I have implemented this feature in EclipseLink by using the same metadata processing framework that is used for dealing with named queries on entities.
 
I'm attaching the code as zip file to this mail :
[ one annoation, one meta data processing class, a patch file showing the changes made to some four or five eclipselink classes, sample entities for testing, and a test case]
 
 
 Please review attached code and let me know how to formally contribute this feature to the eclipselink project.
 
Thanks and Regards,
Samba
 

Attachment: QueryCollection.rar
Description: application/rar


Back to the top