Hi!
After a lot of attempts, I decided to implement my own IUserType.
I've writen the following code:
Code:
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.Type;
namespace MapeamentoOR.Types
{
public class MyTimeSpan : IUserType
{
public MyTimeSpan() { }
public SqlType[] SqlTypes
{
get { return new SqlType[] { new NHibernate.SqlTypes.TimeSqlType() }; }
}
public System.Type ReturnedType
{
get { return typeof(System.TimeSpan); }
}
public bool Equals(object x, object y)
{
return TimeSpan.Equals(x, y);
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
int i = rs.GetOrdinal(names[0]);
if (rs.IsDBNull(i))
return null;
else
{
DateTime dt = Convert.ToDateTime(rs[i]);
return new TimeSpan(dt.Hour, dt.Minute, dt.Second);
}
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null)
((IDbDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
else
{
IDataParameter parm = (IDataParameter)cmd.Parameters[index];
TimeSpan ts = (TimeSpan)value;
parm.Value = new TimeSpan(ts.Hours, ts.Minutes, ts.Seconds);
}
}
public object DeepCopy(object value)
{
return value;
}
public bool IsMutable
{
get { return true; }
}
}
}
I intend to use this type to represent a TIME field from my database. But when I define a field using it, I get the following exception:
Quote:
{"could not interpret type: MapeamentoOR.Types.MyTimeSpan, MapeamentoOR.Types"}
My XML file is below:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="MapeamentoOR.Turno,MapeamentoOR" table="turno">
<composite-id access="field">
<key-property name="EmpId" column="EMP_ID" type="Int32" />
<key-property name="Codigo" column="CODIGO" type="Int32" />
</composite-id>
<property column="DESCRICAO" type="String" name="Descricao" not-null="true" />
<property column="TIPO" type="Int32" name="Tipo" not-null="true" />
<property column="DOMHORAINICIOPARTE1" type="MapeamentoOR.Types.MyTimeSpan, MapeamentoOR.Types" name="Domhorainicioparte1" />
...........
Somebody save me!! :P
Thanks.