Posted by gadominas
on January 22, 2008 at 3:45 PM PST
Do you have a method per named query in you JPADAO? Are you happy with this?
Let's say I have tones of named queries through all my entities. These queries have one/two or more named parameters in conditional expression.
The common way to encapsulate my named query lookup in my JPADAO is something like this:
Query q = %getEntityManager()%.createNamedQuery( %queryName% );
q.setParameter ("someParam", new Date ());
q.setParameter ("anotherParam", true);
q.setParameter ("tired_butAnoterParamX", 666L);
Do we have another way here? What if my named queries are overloaded by entity mapping..
My proposal for myself was to make generic lookup in my base JPADAO, as follows:
1. pass the named query name and named arguments as a array as follows:
%someFactory%.getXJPADAO().executeNamedQuery( "QueryName", param1, param2, .... , paramN);
2. Retrieve the entity type which is associated with the dao.
3. Tokenize the query which we want to execute and build the parameters bind set.
4. Go through parameters bind set and setup the query.
As an example it can be shown as follows:
This example will not cover positional parameters features.
Folks, what do you think. I'm too high from the ground or we have a better way? How you deal with this area?