-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: MSSql 2000 and saving decimal type into money column
PostPosted: Wed Mar 15, 2006 9:17 am 
Newbie

Joined: Thu Aug 25, 2005 3:14 am
Posts: 19
Hi everybody!

I`m using Hibernate version:1.0.2, and
MSSQL 2000 - MSDE

in my table "Ref1" I create column with type - Money.
In my mapping and in code i use type Decimal
to hold data from the column.
Mapping:
Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"  assembly ="NHDomainLib" >
<class name="NHDomainLib.Ref1, NHDomainLib" table="Ref1">
   <id name="Id" column="Id" type="Int32" unsaved-value="0" >
      <generator class="native"/> 
   </id>            
   <property name="MValue" column="mvalue"   type="Decimal"/>
</class>   
</hibernate-mapping>

Class Ref1

[code]
public class Ref1
{
private int _Id;
private decimal _MValue;

public Ref1(){}
public int Id
{
get {return _Id;}
set {_Id =value;}
}
public decimal MValue
{
get {return _MValue;}
set {_MValue = value;}
}

}
[code]

When i want to save instance of Ref1
with property MValue = 1000 000 000 000 00m;
[code]
Ref1 r1= new Ref1();
r1.MValue = 1000 000 000 000 00m;

ITransaction tx = session.BeginTransaction();
try{
session.SaveOrUpdate(r1);
tx.Commit();
}
cacth
{
tx.Rollback()
throw;
}

[/code]


I receive exception


could not insert: [NHDomainLib.Ref1]

at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledIdentityInsertion.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.DoSave(Object theObj, Key key, IClassPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IClassPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj)
at NHDomainLib._Main.Main() in c:\documents\nhdomainlib\_main.cs:line 44

this exception disappear when i save any number less then -10E14 .
in generated sql code

exec sp_executesql N'INSERT INTO Ref1 (mvalue) VALUES (@p0); select SCOPE_IDENTITY()',
N'@p0 decimal(19,5)',
@p0 = -10000000000000.00000

i see that property with type Decimal converted to the parameted
with type decimal(19,5)
BUT THIS IS WRONG transformation!!! In MSSql type Money must be mapped to the type Decimal(19,4) but NOT A Decimal(19,5)!!!

I solved this by changing a small code in file NHibernate\Driver\SqlClientDriver.cs
in method
protected override IDbDataParameter GenerateParameter( IDbCommand command, string name, Parameter parameter, Dialect.Dialect dialect )
code

case DbType.Decimal:
pps = parameter as ParameterPrecisionScale;
dbParam.Precision = 19;
dbParam.Scale = 5;
break;

changed on

case DbType.Decimal:
pps = parameter as ParameterPrecisionScale;
dbParam.Precision = 19;
dbParam.Scale = 4;[color=red]
break;
and after that rebuilding NHIbernate.dll I successfuly save my
entity with value 100000000000000m.

May be exist other way to do this without changing the internals of HNIbernate and rebuilding it ??

PS: I founded very interesting and funny comment in file SqlClientDriver.cs line - 167

case DbType.Decimal:
pps = parameter as ParameterPrecisionScale;
[color=red]TODO: remove this hardcoding...

dbParam.Precision = 19;
dbParam.Scale = 5;

_________________
------------------------------
NETDigitally yours Jericho


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 10:27 am 
Newbie

Joined: Tue Jan 22, 2008 5:51 pm
Posts: 7
Location: Brazil
Yes... u can do this without change internals of nhibernate...

declare on hbm file:

<property name="propertyname" column="columnname" type="Decimal(19,4)" />

and done...


but i think it's a good thing u done, send to nhibernate support, to change that bug on next version!!!!

great work!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.