-
1. Re: @NamedQuery and setting orderBy as a parameter?
fhh Jan 30, 2007 8:36 PM (in response to rocken7)Just from my database experience I would say that it is not possible. Any sane JPA-Provider will execute the query using a PreparedStatement and any sane JDBC driver will excute a prepared statement using bind variables.
AFAIK bind variables can only represent SQL literals like strings, numbers and dates but they can not be replaced by a database object like a column.
So your named query would result in somethingSELECT * FROM TBL_HOT_CONTENT ORDER BY 'viewsWeekly desc'
and this will not work for obvious reasons. Please correct me if I am wrong.
Regards
Felix -
2. Re: @NamedQuery and setting orderBy as a parameter?
rocken7 Jan 31, 2007 12:50 PM (in response to rocken7)EJB3 is an ORM, therefore it shouldn't be so difficult to accept attributes (simple accessor method) for orderBy and then swap them out as it handles the query.
Suppose I haven't written much in the realm of JPA, but some kind of mapping or framework hint should be accounted for this kind of use.
Basically, this is a huge use-case for ejb3 and I'm suprised its not addressed, nor even discussed. Yet its the first thing anyone mentions when they get into implementing some ejb3 app.
Even if I handled the mapping on my end, viewsWeekly == 'c.views_weekly' etc. -
3. Re: @NamedQuery and setting orderBy as a parameter?
fhh Jan 31, 2007 3:25 PM (in response to rocken7)
Basically, this is a huge use-case for ejb3 and I'm suprised its not addressed, nor even discussed. Yet its the first thing anyone mentions when they get into implementing some ejb3 app.
Why is this a huge use case? Simply do something like this:public final String findHottestQuery = "select c FROM Content c" Query q = em.createNamedQuery(findHottestContent + " order by viewsWeekly desc");
If your queries access different database objects (and not just different data) you will not profit from named queries or parametrized statements anyway.
Regards
Felix -
4. Re: @NamedQuery and setting orderBy as a parameter?
mazz Jan 31, 2007 3:31 PM (in response to rocken7)> Query q = em.createNamedQuery(findHottestContent + " order by viewsWeekly desc");
that doesn't work!! That's not a named query.
This is a very big thing missing in the spec, this much is clear. -
5. Re: @NamedQuery and setting orderBy as a parameter?
fhh Feb 1, 2007 12:49 PM (in response to rocken7)
that doesn't work!! That's not a named query.
It is not a named query but it does work. Why are you so keen on using a named query of you are not going to profit from it.
The restriction of not using a bind variable is - as far as I know - a restriction of databases not of the JPA provider. There is a work around for that if you are using Oracle (http://www.oracle.com/webapps/online-help/reports/10.1.2/state/content/navId.3/navSetId._/vtTopicFile.htmlhelp_rwbuild_hs%7Crwwhthow%7Cwhatare%7Cdmobj%7Csq_a_bind_references~htm/)
but to use it you will have to use a native query.
Regards
Felix