You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Sylwia Bugla <sy...@dcc.fc.up.pt> on 2011/09/01 07:58:20 UTC
ClassCastException with SQLResult and SQLTemplate
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
Re: ClassCastException with SQLResult and SQLTemplate
Posted by Sylwia Bugla <sy...@gmail.com>.
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 <an...@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
>>
>
>
Re: ClassCastException with SQLResult and SQLTemplate
Posted by Sylwia Bugla <sy...@dcc.fc.up.pt>.
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 <an...@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 <an...@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
>>>>
>>>
>>>
>>
>
>
Re: ClassCastException with SQLResult and SQLTemplate
Posted by Andrus Adamchik <an...@objectstyle.org>.
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 <an...@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
>>>
>>
>>
>
Re: ClassCastException with SQLResult and SQLTemplate
Posted by Sylwia Bugla <sy...@dcc.fc.up.pt>.
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 <an...@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
>>
>
>
Re: ClassCastException with SQLResult and SQLTemplate
Posted by Andrus Adamchik <an...@objectstyle.org>.
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
>