Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Non-Entity types and TypedQuery / ClassCastExceptions

Thanks Chris,

that indeed did the job. I somehow got lost with the
native query add-on
http://wiki.eclipse.org/EclipseLink/Development/2.1/AdvancedJPA_Queries#Native_Queries_returning_non-Entity_results
and had hoped, that this will work for JPQL queries also..

Thanks again,
- M

On 7 March 2013 14:12, Christopher Delahunt
<christopher.delahunt@xxxxxxxxxx> wrote:
> JPA only understands entities with mappings, so if CountryHolder isn't an entity, it has no way of building what you want from what you've provided.
>
> You can either work with the raw data returned in the object array, or specify the constructor to use in the JPQL like
> entityManager.createQuery("SELECT new CountryHolder(v.country, COUNT(v.country)) cnt FROM Visit v GROUP BY v.country ORDER BY COUNT(v.country)");
>
> Best Regards,
> Chris
>
> On 2013-03-07, at 5:54 AM, Markus Eisele <myfear@xxxxxx> wrote:
>
>> Hi all,
>>
>> I'm playing around with non-entity return types at the moment and
>> constantly run into java.lang.ClassCastExceptions ..
>>
>> Given:
>> TypedQuery<CountryHolder> query = entityManager.createQuery("SELECT
>> v.country, COUNT(v.country) AS cnt FROM Visit v GROUP BY v.country
>> ORDER BY COUNT(v.country)", CountryHolder.class);
>> List<CountryHolder> results = query.getResultList();
>>
>> keeps returning a list of Object[] but not the desired pojo (which has
>> a public non-arg and a  public CountryHolder(String country, Integer
>> cnt) constructor ...)
>>
>> Every try to iterate the List and cast to anything else than Object fails ...
>>
>> Running that query against the DB returns the expected two columns mit
>> the names country and cnt ...
>>
>> Turning sql logging on gives an indication that the following sql is
>> send to the database .. which in fact skips the "as cnt" part ...
>>
>> SELECT COUNTRY, COUNT(COUNTRY) FROM VISIT GROUP BY COUNTRY ORDER BY
>> COUNT(COUNTRY) ASC
>>
>> I'm running GlassFish Server Open Source Edition 3.1.2.2 with Eclipse
>> Persistence Services - 2.3.2.v20111125-r10461 against Apache Derby
>> Network Client JDBC Driver  Version: 10.8.2.2 - (1181258)
>>
>> Any ideas highly appreciated!
>>
>> Thanks,
>> - Markus
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top