4 Replies Latest reply on Aug 16, 2010 7:01 PM by Olalekan Ogunleye

    Using Entity Query

    Olalekan Ogunleye Newbie
      Hi All,

      I am using Entity quesry and it was working fine. However, I wanted to sort my data with this and I seem not to get the correct code as I have exhausted all the methods available but seems not to be working. Please can someone who know how to use this more than I do help me. Please find the code I am using below and possibly tell me what I must do to make sure that the data are sorted after displaying them.

      Thank you in advance.

      package za.co.csir.meraka.mobi4d.web.voting.session;

      import java.util.Arrays;
      import java.util.List;

      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityQuery;

      import za.co.csir.meraka.mobi4d.web.voting.entity.Vote;

      /**
      * Title: Vote Home
      * Description: This is the vote list...
      *          
      * Created on: July 19, 2010
      * Company:     CSIR
      *
      * @author JP Tolmay
      * @version 0.1
      */
      @Name("voteList")
      public class VoteList extends EntityQuery<Vote>
      {
           // region Declarations
           private static final long serialVersionUID = 3200216085422142008L;
           private final static String QUERY = "select vote from Vote vote";
           private final static String[] RESTRICTIONS = { "lower(vote.name) like lower(#{voteList.name})", "lower(vote.topic) like lower(#{voteList.topic})", "lower(vote.feedback) like lower(#{voteList.feedback})" };
           
           // region Constructors
           /**
            * Declare all the constructors.
            */
          public VoteList() { this._Initialize(QUERY, RESTRICTIONS); } // Initialise the class
           
          // region Properties
          /**
           * Holds the name value to include in the list
           */
           public void setName(String name) { this.name = name; }
           public String getName() { return this.name; }
           private String name;
           
           /**
           * Holds the topic value to include in the list
           */
           public void setTopic(String topic) { this.topic = topic; }
           public String getTopic() { return this.topic; }
           private String topic;
           
           /**
           * Holds the feedback value to include in the list
           */
           public void setFeedback(String feedback) { this.feedback = feedback; }
           public String getFeedback() { return this.feedback; }
           private String feedback;
           
           // region Methods

          // region Methods-private
          /**
           * Description: This method initialise the class
           */
          private void _Initialize(String query, String[] restrictions) {
               
                // Set the query and restrictions to use
              this.setEjbql(query);
              this.setRestrictionExpressionStrings(Arrays.asList(restrictions));
           this.setOrderBy(topic);
          }
         
          // region Methods-public
           /**
           * Description: This method returns a list of stored records, dependent on the filtered options
           *
           * @param name Holds the 'vote.name' specific records to filter (find)
           * @param topic Holds the 'vote.topic' specific records to filter (find)
           * @param feedback Holds the 'vote.feedback' specific records to filter (find)
           * @return A list of stored 'vote' objects from the persistence store
           */
           public List<Vote> getList(String name, String topic, String feedback) { this.name = name; this.topic = topic; this.feedback = feedback; return this.getResultList(); }
           
           /**
           * Description: This method returns the number of stored records, dependent on the filtered options
           *
           * @param name Holds the 'vote.name' specific records to filter (find)
           * @param topic Holds the 'vote.topic' specific records to filter (find)
           * @param feedback Holds the 'vote.feedback' specific records to filter (find)
           * @return A list of stored 'vote' objects from the persistence store
           */
           public int getCount(String name, String topic, String feedback) { this.name = name; this.topic = topic; this.feedback = feedback; return this.getResultList().size(); }
      }
        • 1. Re: Using Entity Query
          Leo van den berg Master

          Hi,


          Try cleaning up your code by splitting the criteria stuff from the EntityQuery. You should try to make simple components which do only what threy intended to do (in this case query the database and get you a list with objects). In your case you could even use a definition of the Query in components.xml.


          EntityQuery has a methods which you should override. somthing like this:



                 
          
          @Override
          public String getEjbql() {
          return "select " +
                  "   n.priority, " +
                  "   n.entryTimestamp, " +
                  "   e.uniqueCode, " +
                  "   concat(u.firstName, concat(' ', u.lastName) ) ," +
                  "   m.uniqueOrganisationID , " +
                  "   n.text " +
                  "from EventNote n " +
                  "   left join n.event e " +  
                  "   left join n.user u " +
                  "   left join u.managementCentre m"; 
          }
          
          @Override
          public String getOrder() {
                  return "n.entryTimestamp desc";
          }
          



          A kind of example in components.xml




          <framework:entity-query name="roadsQuery"
                  entity-manager="#{entityManager}" ejbql="select r from Road r"
                  order="r.fullName asc" >
                  <framework:restrictions>
                          <value>r.fullName like #{roadCriteria.wildCard and not empty roadCriteria.road ? roadCriteria.road : null }</value>
                          <value>r.fullName = #{not roadCriteria.wildCard and not empty roadCriteria.road ? roadCriteria.road : null }</value>
                  </framework:restrictions>
                  </framework:entity-query> 
          
          



          The roadCriteria is a simple Seam bean which holds the criteria values. In this specific case we can search for a road and depending on a wildcard boolean we are using the Like or a  eq comparator.


          Hopefully this helps a bit.


          Leo




          • 2. Re: Using Entity Query
            Olalekan Ogunleye Newbie

            Thanks Leo for the response. But to be honest I did not understand your code as I am not a guru in seam usage but I am trying and the community has helped to some extent. However, my code as it is displays the data that I want it to display but I am only having the problem ordering it. If perhaps you can just help me and/or tell me what I must do to the current code to make it order the data.


            Thank you

            • 3. Re: Using Entity Query
              Leo van den berg Master

              Hi,


              just override the getOrder() method and that will sort you result.


              Leo

              • 4. Re: Using Entity Query
                Olalekan Ogunleye Newbie

                Thanks a lot Leo, I did that and my problem is sorted.