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.  [ 11 posts ] 
Author Message
 Post subject: Error when using my IUserType
PostPosted: Fri May 05, 2006 1:59 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 4:36 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We have several IUserType implementations and we were getting the same problem as well. I can't remember offhand how we resolved it, I'll have to look into it. I'm pretty sure we're manually loading the DLL that defines the user type ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 4:45 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
I think we tried manually loading the DLL before the session factory gets created, but it didn't help. I believe there was a bug in the way dynamically loaded types were resolved, which was fixed in 1.0.2. Are you using that version of NHibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 4:46 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We have several IUserType implementations and we were getting the same problem as well. I can't remember offhand how we resolved it, I'll have to look into it. I'm pretty sure we're manually loading the DLL that defines the user type ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 4:48 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Nels_P_Olsen wrote:
We have several IUserType implementations and we were getting the same problem as well. I can't remember offhand how we resolved it, I'll have to look into it. I'm pretty sure we're manually loading the DLL that defines the user type ...


I didn't understand ...

"we're manually loading the DLL that defines the user type..."

My user types are defined into the same class library that defines the mapping. Is it a problem?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 4:50 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Nels_P_Olsen wrote:
I think we tried manually loading the DLL before the session factory gets created, but it didn't help. I believe there was a bug in the way dynamically loaded types were resolved, which was fixed in 1.0.2. Are you using that version of NHibernate?


Yes, I'm using the version 1.0.2.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 5:05 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We ended up putting the user types in the same DLL as our gateway class that creates the NHibernate configuration and session factory. If you don't have a similar central location where the session factory is created, then try creating a dummy instance of your user type in the code that creates the session factory, to guarantee that the DLL and type are loaded beforehand. Note that if you are building in Release mode in Visual Studio, you need to pass this dummy instance to some dummy method, or the compiler will realize that you're not using it and will optimize it out ...


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 9:50 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Nels_P_Olsen wrote:
We ended up putting the user types in the same DLL as our gateway class that creates the NHibernate configuration and session factory. If you don't have a similar central location where the session factory is created, then try creating a dummy instance of your user type in the code that creates the session factory, to guarantee that the DLL and type are loaded beforehand. Note that if you are building in Release mode in Visual Studio, you need to pass this dummy instance to some dummy method, or the compiler will realize that you're not using it and will optimize it out ...


Thank you! ;)

Now my own TimeSpan type is working! :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 4:43 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Nels, and about my code... what do you think about it?

I'd like to get an opinion from yourself!

Thanks ;)


Top
 Profile  
 
 Post subject: Same problemm
PostPosted: Tue Jun 24, 2008 9:18 am 
Newbie

Joined: Wed Jun 18, 2008 10:17 am
Posts: 5
Quote:
Thank you! ;)

Now my own TimeSpan type is working! :)


I have same problem.
Can You post detailed solution to solve this problem.
Because i don't really understand how You have been resolve it.

Please help with solution of this problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 11:26 am 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
The solution was to define the user types in the same DLL that creates the NHibernate session factory. If your creation of the session factory is scattered elsewhere, you'll need to make a front-end for creating it, and put this front end in the same DLL as your user types.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.