2 Replies Latest reply on Aug 13, 2010 9:36 AM by adamk

    EJB-QL - Restrictions with value range

    adamk

      Hello,


      i would like to get all records from address table where 'buildingNr' is between 1 and 20. Eclipse generated my AdresseList class with restrictions and now I would like to add this new restriction to it.


      @Name("sspAdresseList")
      public class SspAdresseList extends EntityQuery<SspAdresse> {
      
           private static final String EJBQL = "select sspAdresse from SspAdresse sspAdresse";
           
           private int hausnrVon;
           private int hausnrBis;
      
           private static final String[] RESTRICTIONS = {
                     "lower(sspAdresse.strasse) like lower(concat(#{sspAdresseList.sspAdresse.strasse},'%'))",
                     "lower(sspAdresse.hausnr) like lower(concat(#{sspAdresseList.sspAdresse.hausnr},'%'))",
                     "lower(sspAdresse.hauszusatz) like lower(concat(#{sspAdresseList.sspAdresse.hauszusatz},'%'))",
                     "lower(sspAdresse.ort) like lower(concat(#{sspAdresseList.sspAdresse.ort},'%'))",
                     "lower(sspAdresse.ortsteil) like lower(concat(#{sspAdresseList.sspAdresse.ortsteil},'%'))", 
                              
                              //this is my additional restriction 
                     "this.hausnrVon < sspAdresse.hausnr  AND sspAdresse.hausnr > this.hausnrBis",
           };
      
           private SspAdresse sspAdresse = new SspAdresse();
      
           public SspAdresseList() {
                setEjbql(EJBQL);
                setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
                setMaxResults(25);
           }
      
      (... all getter and setter ...)
      }



      When i try it out by navigating to the proper mask and filling those 2 fields (min/max) i receive the following exception:




      javax.el.ELException: /SspAdresseList.xhtml: Error reading 'resultList' on type org.domain.dummy2.session.SspAdresseList_$$_javassist_seam_3
           at com.sun.facelets.compiler.TextInstruction.write(TextInstruction.java:48)
           at com.sun.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:39)
           at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:280)
           at org.richfaces.renderkit.html.PanelRenderer.doEncodeBegin(PanelRenderer.java:189)
      (....)
      Caused by: java.lang.IllegalArgumentException: there should be exactly one value binding in a restriction: org.jboss.seam.core.Expressions$1@1e20bca
           at org.jboss.seam.framework.Query.parseEjbql(Query.java:230)
           at org.jboss.seam.framework.EntityQuery.createQuery(EntityQuery.java:175)
           at org.jboss.seam.framework.EntityQuery.initResultList(EntityQuery.java:79)
           at org.jboss.seam.framework.EntityQuery.getResultList(EntityQuery.java:71)






      For any suggestions i will be very thankful


      Merci
      Adam


        • 1. Re: EJB-QL - Restrictions with value range
          lvdberg

          Hi,


          As far as I now, the QL can't make a reference to the bean where it is defined, so just add twoo additional strings referencing context variableS and make sure you outject the needed values.


          Leo


          P.S. I personally prefer to declare these kind of beans in components.xml, because it simplifies the maintenance of your application substantially.


           

          • 2. Re: EJB-QL - Restrictions with value range
            adamk

            Hi Leo,


            you're right. My list class 'SspAdresseList' was not able to resolve those variables. I made 2 mistakes


            Nr1: each restriction has to be a new element


            Wrong:



            (...)
            "this.hausnrVon < sspAdresse.hausnr  AND sspAdresse.hausnr > this.hausnrBis",
            (...)





            Right:



            (...)
            "lower(sspAdresse.plz) >= #{rest.minPlz}", 
            "sspAdresse.plz < #{rest.maxPlz}",
            (...)



            Usage of lower method is optional.



            Nr2: it is not possible to resolve values inside of currently active list class


            SEAM component 'rest' is a simple POJO with int attributes minPlz and maxPlz.



            Thanks a lot Leo!