3 Replies Latest reply on Mar 12, 2009 7:49 AM by jaikiran

    Query returns Object instead of the right type....

      Hi all !
      I have a query which worked fine until now:

      Query dbQuery = em.createQuery("SELECT OBJECT(a) FROM TaskList AS a");
      List <TaskList> list = dbQuery .getResultList();


      Now I need to change the query, just to restrict the query to a few fields from the TaskList:

      Query dbQuery = em.createQuery("SELECT taskId,taskName FROM TaskList AS a");
      List <TaskList> list = dbQuery .getResultList();


      The problem is that now the list contains Objects and not TaskList......I need TaskList otherwise I will have ClassCastExceptions in other pieces of the code......

      is my syntax wrong or what else ?
      Thanks a lot
      marco


        • 1. Re: Query returns Object instead of the right type....
          hanyshafik

          The following query
          SELECT taskId, taskName FROM TaskList AS a
          doesn't return list of tasks instead it will return list of objects, each item in the list describes one row as another list of objects. Each item in the latter list describes one column (e.g. taskName as String).
          if you want it to return list of tasks that has only taskid and taskname populated you can you use the following query
          SELECT new TaskList(taskId, taskName) FROM TaskList AS a
          ofcourse you will need the above constructor for TaskList

          • 2. Re: Query returns Object instead of the right type....

            hello!
            thank you very much for your advice.
            Unfortunately I cannot follow your approach because the number of fields are built at runtime - in other words the user creates at runtime a View of the table.
            I should add all possible constructors (they're about 20 fields :-( )
            maybe I should create a Native Query..if I don't have other options...
            thanks anyway
            marco

            • 3. Re: Query returns Object instead of the right type....
              jaikiran

               

              "dimar1975" wrote:

              The problem is that now the list contains Objects and not TaskList......I need TaskList otherwise I will have ClassCastExceptions in other pieces of the code......



              Its going to return an Object array. You will have to change the return type since you no longer are expecting an entire TaskList from the query output.