1 Reply Latest reply on Feb 4, 2009 3:38 PM by dragonpil

    Override getEJBQL with Parameters

    dragonpil
      Hi I am a real noob in seam and trying to use the generated List sessionobject.

      I like to make a Join between 2 Entities ( Element, Element Synonyms). Also i like to filter the Results with Parameters from a Form. (Elementname and Element NameChem)

      So what i am like to do is to search in Elements for this name  OR in Element_Synonyms

      With Restrictions i dont have a Idea how to make a OR  cause there i only can give one value Binding per Restriction and they will be Concat with an AND.

      I Tryed to Override the getResultList Methode with something like:
      getEntityManager().createQuery(EJBQL).setParameter("name", "#{ParameterElementList.parameterElement.name}").getResultList();

      This worked but every Row in the Database produced a Select statement.

      So i Tryed to Override now the getEBQL methode but i have no Idea how to get the Parameters into the EJBQL i tryed with #{...name} in the whenclause.

      My last try was this one:
      ...
      @Name("parameterElementList")
      public class ParameterElementList extends EntityQuery<ParameterElement> {
           
           @Logger
           static private Log log2;
           
           private static final String EJBQL = "select parameterElement from ParameterElement parameterElement left join fetch parameterElement.parameterElementSynonyms pes";
           private static final String  whereClause = " WHERE " +
                     "(lower(parameterElement.name) like concat(lower(:name),'%') OR "+
                     " lower(pes.id.nameSynonym) like concat(lower(:name),'%'))" +
                     " AND " +
                     "(lower(parameterElement.nameChem) like concat(lower(:nameChem),'%') OR "+
                     " lower(pes.id.nameSynonym) like concat(lower(:nameChem),'%'))";
           private static final String[] RESTRICTIONS = {};

           private ParameterElement parameterElement = new ParameterElement();

           


              public ParameterElementList() {
                
                final List parameterUeber = new ArrayList();
                
                parameterUeber.add(this.parameterElement.getName() != null ? this.parameterElement.getName() : "");
                parameterUeber.add(this.parameterElement.getNameChem()!= null ? this.parameterElement.getNameChem() : "");
                this.setQueryParameterValues(parameterUeber);
                setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
                setMaxResults(25);
                
           }

           @Override
           public String getEjbql() {

                return EJBQL+whereClause;
           }
           
           @Override
           public List<ParameterElement> getResultList() {
                // TODO Auto-generated method stub
                return super.getResultList();
           }

           public ParameterElement getParameterElement() {
                return parameterElement;
           }
      }

      Does somebody could help me with is issue?
        • 1. Re: Override getEJBQL with Parameters
          dragonpil

          I found a Workaround for this issue but i am not sure if this is a valid way.


          i made a private resultListCash and I overwrote the getResultList methode.


               @Override
               public List<ParameterElement> getResultList() {
                    if (this.resultListCash == null || this.resultListCash.size()== 0){
                                this.resultListCash = getEntityManager().
                             createQuery(EJBQL+whereClause)
                                .setParameter("name",this.parameterElement.getName())
                             .setParameter("nameCh",this.parameterElement.getNameChem())
                             .getResultList();
                    }
                    return resultListCash;
               }



          Am i right that for every Call a new Instance of the List object is Created and so the resultListCash is renewed?


          Does somebody has a better Idea?