2 Replies Latest reply on Sep 14, 2006 6:15 AM by fhh

    Dynamic Named Query?

    hotat27

      Hello,

      Currently I'm using named query which is written in orm.xml and is working fine.

      Query query = em.createNamedQuery("selectAddresses");
      query.setParameter("postcode", 50000);
      query.getResultList();
      
      <query>
      select * from address where postcode=:postcode ORDER BY state ASC
      </query>
      

      Is it possible to dynamically change the "ORDER BY state ASC" to ascending or descending in java code, either through query.setParameter("", "") or any other methods?

      I've tried the setParameter method, for example
      <query>select * from address where postcode=:postcode ORDER BY state :ordering</query>
      
      query.setParameter("ordering", "ASC") or query.setParameter("ordering", "DESC")
      

      but the result didn't seem to be consistent.

      Thanks in advance.
      Steven

        • 1. Re: Dynamic Named Query?
          hotat27

          Please help.....

          Doesn't anyone expert on this? Please help...

          Many thanks in advance.....

          • 2. Re: Dynamic Named Query?

             

            <query>select * from address where postcode=:postcode ORDER BY state
            :ordering
            </query>
            
            query.setParameter("ordering", "ASC")
            


            I have not actually checked ut my first guess would be that you will get

            select * from address where postcode=5000 ORDER BY state 'ASC'
            


            which will not work for obvious reasons. Propably the easiest way to solve this is to have two queries and then choose dynamically which one to use.

            Regards

            fhh