You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-cs@ibatis.apache.org by Martin Wickman <ma...@dapresy.com> on 2009/05/08 15:27:31 UTC

iBatis doesn't work with doubles?

Hello

I get "System.InvalidCastException: Specified cast is not valid" and the following stacktrace:

at System.Data.SqlClient.SqlBuffer.get_Double()
at System.Data.SqlClient.SqlDataReader.GetDouble(Int32 i)
at IBatisNet.DataMapper.Commands.DataReaderDecorator.System.Data.IDataRecord.GetDouble(Int32 i)
at IBatisNet.DataMapper.TypeHandlers.DoubleTypeHandler.GetValueByIndex(ResultProperty mapping, IDataReader dataReader)
at IBatisNet.DataMapper.Configuration.ResultMapping.ResultProperty.GetDataBaseValue(IDataReader dataReader)
at IBatisNet.DataMapper.MappedStatements.ResultStrategy.AutoMapStrategy.Process(RequestScope request, IDataReader& reader, Object resultObject)
at IBatisNet.DataMapper.MappedStatements.ResultStrategy.ResultClassStrategy.Process(RequestScope request, IDataReader& reader, Object resultObject)
at IBatisNet.DataMapper.MappedStatements.MappedStatement.RunQueryForList[T](RequestScope request, ISqlMapSession session, Object parameterObject, IList`1 resultObject, RowDelegate`1 rowDelegate)
at IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForList[T](ISqlMapSession session, Object parameterObject)
at IBatisNet.DataMapper.SqlMapper.QueryForList[T](String statementName, Object parameterObject)
at Dapresy.RefDB.ServiceLibrary.DB.FooHelper.select() in C:\...\DB\FooHelper.cs:line 11
at UnitTests.AttributesTest.foo() in C:\...\UnitTests\AttributesTest.cs:line 79

This is a simple as I could make the test case:

Foo.xml (the sqlMap):

<?xml version="1.0" encoding="UTF-8" ?>
<sqlMap
    namespace="Foo"
    xmlns="http://ibatis.apache.org/mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../iBatis/SqlMap.xsd">

    <statements>
        <select id="getFoo" resultClass="Foo">
            SELECT 1.2345 as d                
        </select>
    </statements>
</sqlMap>

Foo.cs (the test class):

    public class Foo
    {
        public double d = 0.0;
    }

mapper().QueryForList<Foo>("getFoo", null);

As you see, this is an extremely simple example and it should of course work. If I change the 'd' property to and int or string or whatever, it works. This is the first time I used doubles with iBats. But using 'float' or 'double' gives this error!

Using iBatis .NET 1.6.[12] and SQL Server2.0.

/Martin

RE: iBatis doesn't work with doubles?

Posted by Martin Wickman <ma...@dapresy.com>.
The problem is that 'SELECT 1' returns an int and 'SELECT 1.2345' returns a Decimal, thus it may vary from time to time depending on the complexity of the SQL clause (ie column types may change). The way I do this now is using 'SELECT CAST(1 AS DOUBLE) as d'. 

Is there some way in iBatis to convert the result to, say, a double in all cases?, ie something like <result coerceInto="double" property="d" column="d"/> 


-------------
From: Vincent Apesa [mailto:vincent.apesa@gmail.com] 
Sent: den 8 maj 2009 19:52
To: user-cs@ibatis.apache.org
Subject: Re: iBatis doesn't work with doubles?

Off the top of my head. You could specify the result map and define the datatype. That should work just fine.
 
<resultMap id="Foo-result" class="Foo">
   <result property="d" column="d" type="double"/>
</result>

   <statements>
       <select id="getFoo" resultMap="Foo-result">
           SELECT 1.2345 as d
       </select>
   </statements>
</sqlMap>
On Fri, May 8, 2009 at 1:42 PM, Christopher DeGuise <ch...@be-pragmatic.com> wrote:
I think (at first inspection) is that you are using a resultClass of "Foo". Change the statement to return the primitive type "double" as the resultClass. The exception is indicating a cast of double->Foo is not valid.

Chris

On Fri, May 8, 2009 at 9:27 AM, Martin Wickman <ma...@dapresy.com> wrote:
Hello

I get "System.InvalidCastException: Specified cast is not valid" and the following stacktrace:

at System.Data.SqlClient.SqlBuffer.get_Double()
at System.Data.SqlClient.SqlDataReader.GetDouble(Int32 i)
at IBatisNet.DataMapper.Commands.DataReaderDecorator.System.Data.IDataRecord.GetDouble(Int32 i)
at IBatisNet.DataMapper.TypeHandlers.DoubleTypeHandler.GetValueByIndex(ResultProperty mapping, IDataReader dataReader)
at IBatisNet.DataMapper.Configuration.ResultMapping.ResultProperty.GetDataBaseValue(IDataReader dataReader)
at IBatisNet.DataMapper.MappedStatements.ResultStrategy.AutoMapStrategy.Process(RequestScope request, IDataReader& reader, Object resultObject)
at IBatisNet.DataMapper.MappedStatements.ResultStrategy.ResultClassStrategy.Process(RequestScope request, IDataReader& reader, Object resultObject)
at IBatisNet.DataMapper.MappedStatements.MappedStatement.RunQueryForList[T](RequestScope request, ISqlMapSession session, Object parameterObject, IList`1 resultObject, RowDelegate`1 rowDelegate)
at IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForList[T](ISqlMapSession session, Object parameterObject)
at IBatisNet.DataMapper.SqlMapper.QueryForList[T](String statementName, Object parameterObject)
at Dapresy.RefDB.ServiceLibrary.DB.FooHelper.select() in C:\...\DB\FooHelper.cs:line 11
at UnitTests.AttributesTest.foo() in C:\...\UnitTests\AttributesTest.cs:line 79

This is a simple as I could make the test case:

Foo.xml (the sqlMap):

<?xml version="1.0" encoding="UTF-8" ?>
<sqlMap
   namespace="Foo"
   xmlns="http://ibatis.apache.org/mapping"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="../iBatis/SqlMap.xsd">

   <statements>
       <select id="getFoo" resultClass="Foo">
           SELECT 1.2345 as d
       </select>
   </statements>
</sqlMap>

Foo.cs (the test class):

   public class Foo
   {
       public double d = 0.0;
   }

mapper().QueryForList<Foo>("getFoo", null);

As you see, this is an extremely simple example and it should of course work. If I change the 'd' property to and int or string or whatever, it works. This is the first time I used doubles with iBats. But using 'float' or 'double' gives this error!

Using iBatis .NET 1.6.[12] and SQL Server2.0.

/Martin



Re: iBatis doesn't work with doubles?

Posted by Vincent Apesa <vi...@gmail.com>.
Off the top of my head. You could specify the result map and define the
datatype. That should work just fine.

<resultMap id="Foo-result" class="Foo">
   <result property="d" column="d" type="double"/>
</result>

   <statements>
       <select id="getFoo" resultMap="Foo-result">
           SELECT 1.2345 as d
       </select>
   </statements>
</sqlMap>

On Fri, May 8, 2009 at 1:42 PM, Christopher DeGuise <
christopher.deguise@be-pragmatic.com> wrote:

> I think (at first inspection) is that you are using a resultClass of "Foo".
> Change the statement to return the primitive type "double" as the
> resultClass. The exception is indicating a cast of double->Foo is not valid.
>
> Chris
>
>
> On Fri, May 8, 2009 at 9:27 AM, Martin Wickman <martin.wickman@dapresy.com
> > wrote:
>
>> Hello
>>
>> I get "System.InvalidCastException: Specified cast is not valid" and the
>> following stacktrace:
>>
>> at System.Data.SqlClient.SqlBuffer.get_Double()
>> at System.Data.SqlClient.SqlDataReader.GetDouble(Int32 i)
>> at
>> IBatisNet.DataMapper.Commands.DataReaderDecorator.System.Data.IDataRecord.GetDouble(Int32
>> i)
>> at
>> IBatisNet.DataMapper.TypeHandlers.DoubleTypeHandler.GetValueByIndex(ResultProperty
>> mapping, IDataReader dataReader)
>> at
>> IBatisNet.DataMapper.Configuration.ResultMapping.ResultProperty.GetDataBaseValue(IDataReader
>> dataReader)
>> at
>> IBatisNet.DataMapper.MappedStatements.ResultStrategy.AutoMapStrategy.Process(RequestScope
>> request, IDataReader& reader, Object resultObject)
>> at
>> IBatisNet.DataMapper.MappedStatements.ResultStrategy.ResultClassStrategy.Process(RequestScope
>> request, IDataReader& reader, Object resultObject)
>> at
>> IBatisNet.DataMapper.MappedStatements.MappedStatement.RunQueryForList[T](RequestScope
>> request, ISqlMapSession session, Object parameterObject, IList`1
>> resultObject, RowDelegate`1 rowDelegate)
>> at
>> IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForList[T](ISqlMapSession
>> session, Object parameterObject)
>> at IBatisNet.DataMapper.SqlMapper.QueryForList[T](String statementName,
>> Object parameterObject)
>> at Dapresy.RefDB.ServiceLibrary.DB.FooHelper.select() in
>> C:\...\DB\FooHelper.cs:line 11
>> at UnitTests.AttributesTest.foo() in
>> C:\...\UnitTests\AttributesTest.cs:line 79
>>
>> This is a simple as I could make the test case:
>>
>> Foo.xml (the sqlMap):
>>
>> <?xml version="1.0" encoding="UTF-8" ?>
>> <sqlMap
>>    namespace="Foo"
>>    xmlns="http://ibatis.apache.org/mapping"
>>    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>    xsi:noNamespaceSchemaLocation="../iBatis/SqlMap.xsd">
>>
>>    <statements>
>>        <select id="getFoo" resultClass="Foo">
>>            SELECT 1.2345 as d
>>        </select>
>>    </statements>
>> </sqlMap>
>>
>> Foo.cs (the test class):
>>
>>    public class Foo
>>    {
>>        public double d = 0.0;
>>    }
>>
>> mapper().QueryForList<Foo>("getFoo", null);
>>
>> As you see, this is an extremely simple example and it should of course
>> work. If I change the 'd' property to and int or string or whatever, it
>> works. This is the first time I used doubles with iBats. But using 'float'
>> or 'double' gives this error!
>>
>> Using iBatis .NET 1.6.[12] and SQL Server2.0.
>>
>> /Martin
>>
>
>

Re: iBatis doesn't work with doubles?

Posted by Christopher DeGuise <ch...@be-pragmatic.com>.
I think (at first inspection) is that you are using a resultClass of "Foo".
Change the statement to return the primitive type "double" as the
resultClass. The exception is indicating a cast of double->Foo is not valid.

Chris


On Fri, May 8, 2009 at 9:27 AM, Martin Wickman
<ma...@dapresy.com>wrote:

> Hello
>
> I get "System.InvalidCastException: Specified cast is not valid" and the
> following stacktrace:
>
> at System.Data.SqlClient.SqlBuffer.get_Double()
> at System.Data.SqlClient.SqlDataReader.GetDouble(Int32 i)
> at
> IBatisNet.DataMapper.Commands.DataReaderDecorator.System.Data.IDataRecord.GetDouble(Int32
> i)
> at
> IBatisNet.DataMapper.TypeHandlers.DoubleTypeHandler.GetValueByIndex(ResultProperty
> mapping, IDataReader dataReader)
> at
> IBatisNet.DataMapper.Configuration.ResultMapping.ResultProperty.GetDataBaseValue(IDataReader
> dataReader)
> at
> IBatisNet.DataMapper.MappedStatements.ResultStrategy.AutoMapStrategy.Process(RequestScope
> request, IDataReader& reader, Object resultObject)
> at
> IBatisNet.DataMapper.MappedStatements.ResultStrategy.ResultClassStrategy.Process(RequestScope
> request, IDataReader& reader, Object resultObject)
> at
> IBatisNet.DataMapper.MappedStatements.MappedStatement.RunQueryForList[T](RequestScope
> request, ISqlMapSession session, Object parameterObject, IList`1
> resultObject, RowDelegate`1 rowDelegate)
> at
> IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForList[T](ISqlMapSession
> session, Object parameterObject)
> at IBatisNet.DataMapper.SqlMapper.QueryForList[T](String statementName,
> Object parameterObject)
> at Dapresy.RefDB.ServiceLibrary.DB.FooHelper.select() in
> C:\...\DB\FooHelper.cs:line 11
> at UnitTests.AttributesTest.foo() in
> C:\...\UnitTests\AttributesTest.cs:line 79
>
> This is a simple as I could make the test case:
>
> Foo.xml (the sqlMap):
>
> <?xml version="1.0" encoding="UTF-8" ?>
> <sqlMap
>    namespace="Foo"
>    xmlns="http://ibatis.apache.org/mapping"
>    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>    xsi:noNamespaceSchemaLocation="../iBatis/SqlMap.xsd">
>
>    <statements>
>        <select id="getFoo" resultClass="Foo">
>            SELECT 1.2345 as d
>        </select>
>    </statements>
> </sqlMap>
>
> Foo.cs (the test class):
>
>    public class Foo
>    {
>        public double d = 0.0;
>    }
>
> mapper().QueryForList<Foo>("getFoo", null);
>
> As you see, this is an extremely simple example and it should of course
> work. If I change the 'd' property to and int or string or whatever, it
> works. This is the first time I used doubles with iBats. But using 'float'
> or 'double' gives this error!
>
> Using iBatis .NET 1.6.[12] and SQL Server2.0.
>
> /Martin
>