ibatis-user-cs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tech fan <monsterc...@gmail.com>
Subject Re: QueryForDictionary<K,V>, do I really have to creae a class for this?
Date Sat, 08 May 2010 15:22:32 GMT
Hi Dave, thanks for the info. But it does not work :(

Got it figured out by debugging into the src. It turned out that Ibatis is
trying to treat whatever you pass in to "keyProperty"/"valueProperty" as a
property of some objects of some type, which, if you don't define, is the
raw data "object[]". You are right about  Ibatis providing a way to get over
this without creating a new type, but I don't think it's documented anywhere
(or am I wrong?). In this case, you need to pass in "[0]",
"[1]" separately to get the values back.

The drawback of this "no new type" way is, the data contained in the object
array is really raw data, without type  conversion. So if you must be very
careful about the types (e.g. if DB is int and you need long, then u'll get
an error similar to "type cast error")

Hope this helps whoever met this wall like me.


On Fri, May 7, 2010 at 4:09 PM, Dave Curylo <curylod@asme.org> wrote:

>  Try either changing your statement to:
> or specifiying the "VALUE" property as the keyProperty parameter like this:
> QueryForDictionary<int,string>("TestQueryForDictionary", null, "VALUE")
>  On May 7, 2010, at 3:55 AM, tech fan wrote:
>  Hi, thanks for the info.
> The table contains just 2 columns, 'value': int, 'description': varchar.
> Here is the map:
> <?xml version="1.0" encoding="utf-8" ?>
> <sqlMap namespace="TestParameters" xmlns="http://ibatis.apache.org/mapping"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
>   <statements>
>     <select id="TestQueryForDictionary">
>     </select>
>   </statements>
>   </sqlMap>
> Here is the code:
>         [Test]
>         public void Test_QueryForDictionary()
>         {
>             IDictionary<int, string> r = _db.QueryForDictionary<int,
> string>("TestQueryForDictionary", null, "CODE");
>             Assert.That(r.Count, Is.EqualTo(2));
>         }
> I got an error like this:
> IBatisNet.Common.Exceptions.ProbeException : There is no Get member named 'CODE' in class
> I guess I need to put resultClass parameter or something in the map, but I
> don't want to create a type for that :(
> Regards,
> tc.
> On Thu, May 6, 2010 at 11:02 PM, Dave Curylo <curylod@asme.org> wrote:
>>  Can you include a snippet of your code?  QueryForDictionary should do
>> what you described without the need to create a new class.
>>  *From:* tech fan <monstercorp@gmail.com>
>> *Sent:* Thursday, May 06, 2010 4:26 PM
>> *To:* user-cs@ibatis.apache.org
>> *Subject:* QueryForDictionary<K,V>, do I really have to creae a class for
>> this?
>> Hi,
>> I have a table with just a integer as pk and a string as description. I
>> need to retrieve all data into a IDictionary<int, string>(this table is
>> human mantained), but I really don't want to creaet a class just for this
>> simple task. But it seems QueryForDictionary converts the result to an
>> object before I can access it. Is there any solution to this? I tried to use
>> set the result class to
>> System.Collections.Generic.Dictionary`2[System.UInt16,System.String] with no
>> luck.
>> Thanks a have a nice day.
>> tc.

View raw message