cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sylwia Bugla <syl-...@dcc.fc.up.pt>
Subject Re: ClassCastException with SQLResult and SQLTemplate
Date Thu, 01 Sep 2011 14:35:54 GMT
OK. Got it!  I removed the SQLResult and than cast the Objects to DataRow:
String sql = "SELECT a.author_id X, count(*) CNT FROM
 `publication_authors` a WHERE a.`cid` != 0 and a.cid = 1234  group by
 a.author_id ORDER BY a.`cid` ASC";

            SQLTemplate query = new
 SQLTemplate(PublicationAuthors.class, sql);
            query.setFetchingDataRows(true);

            List objects =  context.performQuery(query);

            for(Object o: objects){
                DataRow dr = (DataRow) o;
                System.out.println(dr.keySet() + "X:"+ dr.get("X") +"
CNT: "+dr.get("CNT"));
            }


I thought it is necessary to use the SQLResult when the columns are
not exact as in the DB table. All the examples I found show it this
way. It was much easier than I though.
Thank you Andrus!

On 1 September 2011 15:05, Andrus Adamchik <andrus@objectstyle.org> wrote:
> Ah got it, the exception is in your code, not the Cayenne code.
>
> Your SQLResult defines the result as Object[], so this is what you are getting here.
You can either keep casting to Map and remove the SQLResult code. Or keep using SQLResult
and cast to Object[]
>
> Andrus
>
> On Sep 1, 2011, at 9:40 AM, Sylwia Bugla wrote:
>
>> The exception stack is nothing more then:
>> Exception in thread "main" java.lang.ClassCastException:
>> [Ljava.lang.Object; cannot be cast to java.util.Map
>> on the line:
>> Map dr = (Map) o;
>>
>> I thought that if the SQLTemplate is setted to a Persistent object
>> (PublicationAuthors.class in this case) than the result is going to be
>> a List of this Persistent objects. In this case it returns Object and
>> I can not cast it to anything else. After adding the two column
>> results (X and CNT) as a SQLResult it just doesn't work any more.
>> I don' t know now how can I get the results of the two extra columns X and CNT?
>>
>> Thank you.
>>
>> On 1 September 2011 13:37, Andrus Adamchik <andrus@objectstyle.org> wrote:
>>> Could you please post the exception stack trace?
>>>
>>> On Sep 1, 2011, at 1:58 AM, Sylwia Bugla wrote:
>>>
>>>> Hello everyone.
>>>> I am getting desperate about this issue. I have a simple piece of code
>>>> whose purpose is to make some counts in the database. It goes like
>>>> this:
>>>>
>>>> String sql = "SELECT a.author_id X, count(*) CNT FROM
>>>> `publication_authors` a WHERE a.`cid` != 0 and a.cid = 1234  group by
>>>> a.author_id ORDER BY a.`cid` ASC";
>>>>
>>>>            SQLTemplate query = new
>>>> SQLTemplate(PublicationAuthors.class, sql);
>>>>            query.setFetchingDataRows(true);
>>>>
>>>>            SQLResult resultDescriptor = new SQLResult();
>>>>            resultDescriptor.addColumnResult("X");
>>>>            resultDescriptor.addColumnResult("CNT");
>>>>            query.setResult(resultDescriptor);
>>>>
>>>>            List objects =  context.performQuery(query);
>>>>
>>>>            for(Object o: objects){
>>>>                Map dr = (Map) o; //tried with PublicationAuthors,
>>>> DataMap => always getting an exeption here "ClassCastException"
>>>>                System.out.println(dr.keySet());
>>>>            }
>>>>
>>>> Could someone explain me what am I doing wrong in here?
>>>> Thanks
>>>> Sylwia
>>>>
>>>
>>>
>>
>
>

Mime
View raw message