[jboss-user] [EJB 3.0] - EntityManager's Native Query & Paging Results

rabbiaqaswar do-not-reply at jboss.com
Fri Jun 13 02:58:05 EDT 2008


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);


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?

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4157872#4157872

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4157872

More information about the jboss-user mailing list