You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by "Jeremy Bauer (JIRA)" <ji...@apache.org> on 2010/02/09 17:35:28 UTC

[jira] Commented: (OPENJPA-1499) java.lang.ClassCastException: [Ljava.lang.Object;

    [ https://issues.apache.org/jira/browse/OPENJPA-1499?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12831527#action_12831527 ] 

Jeremy Bauer commented on OPENJPA-1499:
---------------------------------------

Your query is returning individual fields of an entity instead of the entity itself.  That results in an invalid cast since the code expecting the result list to be a list of GazTown entities.  If you only want the individual fields of the entity you'll need to process them separately.  If you expect the result to contain GazTown entities modify your query as follows.

Query q = em2.createQuery("SELECT f FROM GazTown f").setMaxResults(5); 

Otherwise, use a list of type of Object[]:

List<Object[]> results = q.getResultList();

Each item in the list will contain a 3 element array which maps to the elements in your selection (f.gazCountyCode,f.gazTownCode,f.gazTownName).

hth,
-Jeremy

> java.lang.ClassCastException: [Ljava.lang.Object;
> -------------------------------------------------
>
>                 Key: OPENJPA-1499
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-1499
>             Project: OpenJPA
>          Issue Type: Bug
>         Environment: openjpa-1.2.2.jar, Oracle10G
>            Reporter: Rajeev Chaudhary
>   Original Estimate: 96h
>  Remaining Estimate: 96h
>
> Size::5
> <Feb 5, 2010 3:16:24 PM EST> <Error> <HTTP> <BEA-101020> <[weblogic.servlet.internal.WebAppServletContext@10ca5bd - appName: 'TestOpenJpa', name: 'TestOpenJpa.war', context-path: '/TestOpenJpa', spec-version: '2.5'] Servlet failed with Exception
> java.lang.ClassCastException: [Ljava.lang.Object;
> 	at org.wadsworth.bean.CheckMe.getData(CheckMe.java:26)
> 	at org.wadsworth.eclep.servlet.FrontContoller.doGet(FrontContoller.java:32)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
> 	at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
> 	Truncated. see log file for complete stacktrace
> public class CheckMe {
> 	@SuppressWarnings("unchecked")
> 	public static void getData(){
> 		EntityManagerFactory factory = Persistence.createEntityManagerFactory("hellojpa");
> 		EntityManager em2 = factory.createEntityManager();
> 		Query q = em2.createQuery("SELECT f.gazCountyCode,f.gazTownCode,f.gazTownName FROM GazTown f").setMaxResults(5);
> 		System.out.println("Size::"+q.getResultList().size());
> 		List<GazTown> results = q.getResultList();   ----> ClassCastException here
> 		System.out.println(results.get(0).toString()+"<<< Sizeoo >>>::::::"+q.getResultList().size());
> 		for (GazTown m : results) {
> 			System.out.println("Success:::"+m.getGazTownName());
> 		}
> 		em2.close();
>         factory.close();
> 	}
> Note: I am getting the result and the size is being shown as 5. But when I cast the resultset, then I get the exception.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.