1 Reply Latest reply on Oct 13, 2006 1:26 PM by cocampo

    Query that selects all rows.

    jimjamz

      Can somebody help me create a query that selects all rows within a table. I'm trying to build an SelectItems boject that contains all the states within the US. I get a ClassCastException each time I try to render the page that has my selectitem component. I looked at the jboss-seam-booking.ear and it is similar to what the BookListAction.java class, but I don't have an @In value and when I do create an @In value of state it gives me another exception.

      I created a local EJB called StateList.java

      @javax.ejb.Local
      public interface StateList{
       public List<State> list();
      }
      


      Then I try to implement it in StateListAction.java
      public class StateListAction implements StateList{
       @PersistenceContext
       EntityManager em;
       .................
       public List<State> list(){
       List<State> list = em.createQuery("select id, name from State");
       ...
       or
       List<State> list = em.createQuery("from State");
       return list;
       }
      }


        • 1. Re: Query that selects all rows.

          First, take a look at this code:

          @Stateful
          @Scope(SESSION)
          @Name("messageManager")
          public class MessageManagerBean implements Serializable, MessageManager
          {
          
           @DataModel
           private List<Message> messageList;
          
           @DataModelSelection
           @Out(required=false)
           private Message message;
          
           @PersistenceContext(type=EXTENDED)
           private EntityManager em;
          
           @Factory("messageList")
           public void findMessages()
           {
           messageList = em.createQuery("from Message msg order by msg.datetime desc").getResultList();
           }
          


          As you may see near the end, there is an invocation to the getResultList method, which gives you the list you're looking for.

          Second, next time you have a question like this, please take a look at the manual, since this code is in Chapter 1!!!. If you keep asking things like this you will be soon ignored (maybe ppl is already ignoring you).
          Please, take your time to read the manual, which is very well documented, read the Wikis (Getting Started, FAQ's, etc.), study the examples, and ONLY THEN come and post your questions (after searching for existing answers to the same problem, of course ;)).

          Regards.