3 Replies Latest reply on Jan 24, 2011 9:32 PM by monkeyden

    EntityQuery - restriction based on context value

    sirseam

      Hello, I have an EntityQuery - 'addressList' generated by Seam-gen for Entity 'Address'.


      Lets say that address has following fields : 'streetName' and 'city', so Seam-gen generate restrictions for them.


      I would like to extend my EntityQuery with a following restrictions: I have an additional field 'keyVal' which can be set in faces context with '{addressList.keyVal}'. Lets supose that 'keyVal' is aaa bbb ccc. Now the query should add restrictions which will be used to find all entities with:


      streetName like '%aaa%' or streetName like '%bbb%' or streetName like '%ccc%' or city like '%aaa%' or city like '%bbb%' or city like '%ccc%'.


      Do you have any suggestions how to achieve this? I am really stucked.

        • 1. Re: EntityQuery - restriction based on context value
          monkeyden

          A fewthings to mention:


          First, I'm not sure how seam-gen creates it but I generally have a criteria object instance (an instance of the entity) to hold criteria.


          private Address criteria;
          
          public get/setCriteria(){...}
          



          Second, I only have a trailing wildcard in my code but I think Hibernate concat takes any number of args.


          lower(address.keyVal) like concat('%', lower(#{addressList.criteria.keyVal}),'%')



          Third, if it's not a persistent field, be sure to use the @Transient annotation.


          Hope it helps

          • 2. Re: EntityQuery - restriction based on context value
            sirseam

            Hello,
            thanks for reply, but i want to create restriction that will strictly depend on value of 'keyVal'.
            As i wrote the restriction should depend on the words in 'keyVal', not on the whole 'keyVal'...

            • 3. Re: EntityQuery - restriction based on context value
              monkeyden

              Ok, I see what you mean.  It's an 'OR' on all the values parsed from keyVal.  You're going to have to:


              1.  Parse the string and build that expression
              2.  Add it to the array of expressions
              3.  Set them in the base class, like so:


              setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
              


              ...where RESTRICTIONS is an String array of your EL expressions.  I typically do this in the constructor but I don't know if you have the value of keyVal at that point.  If not, you'll have to do it further downstream.  Just don't build RESTRICTIONSs at query execution time (e.g. in getResultList()).