Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

EntityManager Native Query & Result paging

843830Jun 12 2008 — edited Jun 15 2008
Hello

I am working with EJB3 on JBoss 4.2.2 GA. In one of the SessionBeans there is a query (sql) which i am running using the createNativeQuery method of the EntityManager.

The query is supposed to return 20 Ids of an Entity. The query returns all 20 Ids. Now if i want to use paging (see only the last 10 records) through the setFirstResult and setMaxResults method of Query, i do this:
query = em.createNativeQuery(strQuery.toString());
query.setFirstResult(10);
query.setMaxResults(10);
then:
ArrayList ids = (ArrayList) query.getResultList();
         System.out.println("no of users found: "+ids.size());      

                if(ids  != null && !ids .isEmpty()){
                         for (Iterator it = ids.iterator(); it.hasNext();) {
                            BigDecimal userId= (BigDecimal)it.next();
                            System.out.println("userid: "+userId);
                        }
                }
In this case the query finds 10 records as the ids.size method gives 10 but then in the loop it gives this exception i.e.
03:09:47,451 ERROR [STDERR] java.lang.ClassCastException: [Ljava.lang.Object;
03:09:47,451 ERROR [STDERR]     at com.server.servicefacades.UserSessionBean.searchUser(UserSessionBean.java:802)
03:09:47,451 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
I suppose the ids ArrayList contains Object arrays instead of containing the userId somehow but i donot understand why. Help please?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 13 2008
Added on Jun 12 2008
2 comments
785 views