-->
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: IUserType based on TimestampType?
PostPosted: Tue Oct 24, 2006 12:54 pm 
Newbie

Joined: Tue Oct 24, 2006 12:44 pm
Posts: 8
Hi,

I want to create an IUserType that will convert a given DateTime to UTC before writing the value to the database.

The problems is that the precision of DateTimeType is not sufficient. I do need milliseconds. So, I want to use TimeStampType but I don't know how to use it within a IUserType.

Thanks
neo

[b]Hibernate version:[/b] 1.0.2.0
[b]Name and version of the database you are using:[/b] Oracle 9i


Top
 Profile  
 
 Post subject: How to create MyCustomUtcTimestampType
PostPosted: Wed Oct 25, 2006 4:24 am 
Newbie

Joined: Tue Oct 24, 2006 12:44 pm
Posts: 8
While trying to solve my problem i have searched for a solution with the help of google. How to create an IUserType has been documented very well and it was very easy to convert a DateTime object to UTC. But the mapping to TimestampType was little difficult.

After little search within the source code of NHibernate I looked over NHibernateUtil class that provides public propertys for the internal NHibernate type constructors (the internal types like TimestampType do not provide public constructor). So I decided to convert my UTC DateTime to TimestampTime.

The following code will show my implementation.

Code:
using System;
using System.Data;
using NHibernate;
using NHibernate.SqlTypes;

namespace MyCompany.Project.Package
{
    public class MyCustomUtcTimestamp : IUserType
    {
        public new bool Equals(object x, object y)
        {  ...  }

        public SqlType[] SqlTypes
        {  get { return {new DateTimeSqlType()}; }  }

        public Type ReturnedType
        {  get { return typeof (DateTime); }  }

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {  ...  }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (((DateTime) value).Equals(default(DateTime)))
            {
                ((IDataParameter) cmd.Parameters[index]).Value = DBNull.Value;
                return;
            }

            ((IDataParameter) cmd.Parameters[index]).Value =
                NHibernateUtil.Timestamp.DeepCopy(((DateTime) value).ToUniversalTime());
        }

        public object DeepCopy(object value)
        {  return value; }

        public bool IsMutable
        { get { return true; }  }
    }
}


Have fun
neo


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.