Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 330846

Summary: A facility to group named queries at a central place
Product: z_Archived Reporter: Samba Kolusu <saasira>
Component: EclipselinkAssignee: Project Inbox <eclipselink.orm-inbox>
Status: NEW --- QA Contact:
Severity: enhancement    
Priority: P3 CC: douglas.clarke, eclipselink.orm-inbox, gordon.yorke, guy.pelletier, jamesssss, ljnelson, michael.f.obrien, saasira, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
URL: http://wiki.eclipse.org/EclispeLink/Development/MetadataCollection
Whiteboard: submitted_patch
Bug Depends on:    
Bug Blocks: 325781    
Attachments:
Description Flags
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
none
Unmodified diff of above user-submitted-patch from inside ZIP attachment
none
This Zip file contains patch file that included added entities and also minor changes in the test class.
saasira: review?
The updated patch file for easier reading none

Description Samba Kolusu CLA 2010-11-22 14:05:19 EST
Build Identifier: 

It is difficult to maintain named queries annotated on each entity and there is no way to define all the named queries at a central place.

The changes provided in the attachment to this bug will enable eclipselink users to group thier named queries at a cental place like a constants file so that it is easier to maintain.

An example would be something like:
 
@QueryCollection(
   namedQueries={

   @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")
  },
  namedNativeQueries={

  },
  nameStoredProcedureQueries={

  },
  resultSetMappings={

  }
  
)
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 bug:
[ 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 see if this feature can be made part of eclipselink project.
 
Thanks and Regards,
Samba

Reproducible: Always

Steps to Reproduce:
1.Named Queries annotated on each entity are difficult to maintain over a period 
  of time since it is difficult to find the entity on which the query is 
  annotated as also finding a proper entity to annotate the query on in case of 
  entitties fetching results from multiple entities or non-entity results.
2.Named Queries annotated on ordinary classes are not processed and hence not 
  usable while executing the queries
Comment 1 Samba Kolusu CLA 2010-11-22 14:13:57 EST
Created attachment 183594 [details]
 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

The attachment contains :

1. An annotation named 'QueryCollection' that can be used on plain java classes to 
   group different forms of named queries.
2. An annotation processor that is used process the QueryCollection annotation and 
   add the annotated queries into the session.
3. A test case to verify that the grouping of named queries functionality is 
   indeed working.
4. A few JPA entites used for testing.

Note:
As of now, only the annotation processing is implemented for the QueryCollection feature; XML processing is yet to be done.
Comment 2 Michael OBrien CLA 2010-11-22 14:37:43 EST
Created attachment 183596 [details]
Unmodified diff of above user-submitted-patch from inside ZIP attachment
Comment 3 Michael OBrien CLA 2010-11-22 15:52:39 EST
Comment on attachment 183596 [details]
Unmodified diff of above user-submitted-patch from inside ZIP attachment

>iplog+ set on submission only
Comment 4 Samba Kolusu CLA 2010-11-22 16:45:17 EST
Created attachment 183612 [details]
This Zip file contains patch file that included added entities and also minor changes in the test class.

1. Updated the .patch file and made its name shorter
2. The test class was having some issue in the setup method where I was calling 
   System.exit() when the entitymanager is closed; Now, I changed it to 
   Assert.fail() so that further tests will fail all at once in case entity 
   manager is not open.
Comment 5 Samba Kolusu CLA 2010-11-22 16:47:29 EST
Created attachment 183613 [details]
The updated patch file for easier reading

This is the same patch file contained in the updated zip file, however adding it separately so that it will be easier to read.
Comment 6 Samba Kolusu CLA 2010-11-22 16:53:09 EST
Comment on attachment 183612 [details]
This Zip file contains patch file that included added entities and also minor changes in the test class.

I think I did not check the review flag while updating the attachment, I got a mail saying that I canelled the review request.

So, requesting the review again; sorry for spamming.
Comment 7 Guy Pelletier CLA 2010-11-24 14:23:10 EST
Samba, thanks for the patch.

What you have proposed is certainly interesting and hits a point that has been
mildly discussed and/or mentioned in the past. That is, providing a means to 
group global type metadata (queries, generators, converters, etc.) in one 
central, class based, location. This is similar to the existing 
<entity-mappings> level, where global metadata can be captured in one mapping 
file. Also, some users have previously used a MappedSuperclass to achieve this.

To proceed with your suggestion, we would need to go through the formal steps of 
developing a new feature. The first steps would be to outline the new 
annotations and their XML counterparts and their usecases in a functional/design 
doc. As an example, see: 

http://wiki.eclipse.org/EclispeLink/Development/AdditionalCriteria

This documentation would then need to be reviewed by several key EclipseLink 
committers to determine its feasibility and complete functionality, naming etc.

In the mean time, I have added your bug as a dependent to our main bug that
encompasses new features through the eclipselink-orm.xml to extend the JPA spec.
Comment 8 Samba Kolusu CLA 2010-11-30 15:23:02 EST
(In reply to comment #7)
Hi Gordan,
   I added a wiki document describing this feature for grouping related non-local metadata at a central place. Although the current bug talks only about grouping
named queries, the design document describes a broader feature enhancement for grouping any non-local metadata and hence the annotation is named @MetadataCollection to represent a generic facility to aggregate metadata.

I have attached the wiki document to this bug as the value of URL field.
Please initiate the review process if the design doc and initial implementation meets the required standards.
Comment 9 Samba Kolusu CLA 2010-11-30 15:27:48 EST
sorry Guy, please accept my apologies; I wrongly addressed the previous 
response to Gordon.
Comment 10 Guy Pelletier CLA 2011-01-31 14:24:38 EST
*** Bug 282047 has been marked as a duplicate of this bug. ***
Comment 11 Eclipse Webmaster CLA 2022-06-09 10:04:28 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink