1 Reply Latest reply on Mar 8, 2008 3:53 AM by Richard Stanford

    Cast an SqlResultSetMapping class

    Laszlo Graf Newbie

      Hi all,

      I have an SqlResultSetMapping like this one:

      @SqlResultSetMapping(
      name="getData01",
      entities={@EntityResult(entityClass=Klass01.class,
       fields={@FieldResult(name="id", column="id"),
       @FieldResult(name="datum", column="datum")
       }),
       @EntityResult(entityClass=Klass02.class,
       fields={@FieldResult(name="col1", column="col1"),
       @FieldResult(name="col2", column="col2")
       })
       }
      )
      


      and I use it in this Query:

      Query q01 = createNativeQuery(queryString, "getData01");
      


      where queryString looks like this:

      select klass01.id, klass01.datum,
       klass02.col1, klass02.col2
       from klass01,
       klass02
       where klass01.id = klass02.col1
      


      Until now OK, it works fine.
      OK, I have the List with the results, but how can I
      access the id, datum, col1 and col2 fields?

      What is the class that I have to use to cast the base
      object of the List?

      Please help me, thank you.
      Laci


        • 1. Re: Cast an SqlResultSetMapping class
          Richard Stanford Newbie

          Should be something like the following:

          Query q01 = createNativeQuery(queryString, "getData01");
          for (Object[] arr : q01.getResultList()) {
           Klass01 k01 = (Klass01)arr[0];
           Klass02 k02 = (Klass02)arr[1];
           // business logic goes here
          }


          When dealing with odd mappings, you can always throw your code into the debugger and check the actual return values as well, then make sure that your code is referring to the appropriate classes or interfaces.