Here's sample code that may work for you. I did not test this at all and it may fail miserably. So, you will need to do the rest of the work.
You will need to specify in your HBM file the use of this type as follows:
Code:
<property name="TimestampPropertyName" type="YourNamespace.UserType.CharTimestampType, YourAssemblyName" />
The code for the user type follows:
Code:
using System;
using System.Data;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.Util;
namespace YourNamespace.UserType
{
[Serializable]
public class CharTimestampType : IUserType
{
#region IUserType Implementation
/// <summary>
/// The SQL types for the columns mapped by this type.
/// </summary>
public SqlType[] SqlTypes
{
get { return new SqlType[] { NHibernateUtil.Timestamp.SqlType }; }
}
/// <summary>
/// The type returned by <see cref="NullSafeGet"/>.
/// </summary>
public Type ReturnedType
{
get { return typeof(DateTime); }
}
/// <summary>
/// Compare two instances of the class mapped by this type for persistent "equality"
/// ie. equality of persistent state.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public new bool Equals(object x, object y)
{
return ObjectUtils.Equals(x, y);
}
/// <summary>
/// Get a hashcode for the instance, consistent with persistence "equality"
/// </summary>
/// <remarks>Required for NHibernate 1.2 Beta 2.</remarks>
public int GetHashCode(object obj)
{
return ((null == obj) ? GetHashCode() : obj.GetHashCode());
}
/// <summary>
/// Retrieve an instance of the mapped class from a result set.
/// </summary>
/// <remarks>Implementors should handle possibility of null values.</remarks>
/// <param name="rs">result set containing the data to evaluate.</param>
/// <param name="names">column names.</param>
/// <param name="owner">the containing entity.</param>
/// <returns></returns>
/// <exception cref="HibernateException">HibernateException</exception>
/// <exception cref="System.Data.SqlClient.SqlException">SqlException</exception>
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
object val = null;
int index = rs.GetOrdinal(names[0]);
if (!rs.IsDBNull(index))
{
try
{
// TODO: This is where you may need to do some coversion to ensure the string representation
// of your Timestamp is in the proper format for what you need. Remember that you will
// need to convert whatever format/type is in rs[index] to a DateTime.
val = rs[index];
}
catch (InvalidCastException ex)
{
throw new ADOException(
"Could not cast the value in field " + names[0] + " of type " + rs[index].GetType().Name + " to the Type " + GetType().Name +
". Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type.", ex);
}
}
return val;
}
/// <summary>
/// Write an instance of the mapped class to a prepared statement.
/// </summary>
/// <remarks>
/// Implementors should handle possibility of <c>null</c> values. A multi-column type should be written
/// to parameters starting from <paramref name="index"/>.
/// </remarks>
/// <param name="cmd">a IDbCommand</param>
/// <param name="value">the object to write</param>
/// <param name="index">command parameter index</param>
/// <exception cref="HibernateException">HibernateException</exception>
/// <exception cref="System.Data.SqlClient.SqlException">SqlException</exception>
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
// TODO: This is where you may need to convert your Timestamp to whatever representation
// you may need to ensure it is stored in your database.
((IDataParameter) cmd.Parameters[index]).Value = ((null == value) ? DBNull.Value : value);
}
/// <summary>
/// Return a deep copy of the persistent state, stopping at entities and at collections.
/// </summary>
/// <param name="value">generally a collection element or entity field.</param>
/// <returns>a copy.</returns>
public object DeepCopy(object value)
{
return NHibernateUtil.Timestamp.DeepCopy(value);
}
/// <summary>
/// Are objects of this type mutable?
/// </summary>
public bool IsMutable
{
get { return NHibernateUtil.Timestamp.IsMutable; }
}
#endregion IUserType Implementation
}
}
Good luck! Hope this helps and it does not throw you in a wild goose chase ;).