openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeremy Bauer (JIRA)" <j...@apache.org>
Subject [jira] Commented: (OPENJPA-1499) java.lang.ClassCastException: [Ljava.lang.Object;
Date Tue, 09 Feb 2010 16:35:28 GMT

    [ 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.


Mime
View raw message