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.  [ 10 posts ] 
Author Message
 Post subject: Equivalent of to_Char function of Oracle in NHibernate
PostPosted: Thu Jan 04, 2007 5:06 am 
Beginner
Beginner

Joined: Fri Dec 01, 2006 5:16 am
Posts: 23
Location: India
Hi,

Could anyone help me in finding an Equivalent of to_Char function of Oracle in NHibernate.


Thankz,
Raul


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 8:06 am 
Beginner
Beginner

Joined: Tue Nov 28, 2006 4:26 pm
Posts: 32
Location: Montreal, Quebec, Canada
Would it be possible for you to describe the scenario you are trying to cover? It may help provide the correct answer to the problem you are trying to solve.


Top
 Profile  
 
 Post subject: Re: Equivalent of to_Char function of Oracle in NHibernate
PostPosted: Fri Jan 05, 2007 3:52 am 
Beginner
Beginner

Joined: Fri Dec 01, 2006 5:16 am
Posts: 23
Location: India
My Oracle client version is 8.1.7 and the server is 10g.In my hbm file I have used TimeStamp datatype which is not undertood by Orcle 8.1.7.The C# classes also have the same datatype for the corresponding properties.
Now the problem is that if I use to_char function to convert the Timestamp datatype in SQL query,it works fine but here I am using HQL or Hibernate Query Language.Here I am not being able to specify the to_char function and hence getting error.


Pls help me out.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 5:10 am 
Beginner
Beginner

Joined: Tue Nov 28, 2006 4:26 pm
Posts: 32
Location: Montreal, Quebec, Canada
It looks like you will need a user type to handle this conversion. See the documentation for IUserType. I don't think you may be able to get away with using substitutions in this case. Nevertheless, take a look at substitutions in the documentation.

Without having code to run, I am assuming that you have a similar problem I had at some point with Oracle where I had to do something similar for booleans.

I think your problem may be in the TimestampType.cs Set method. Not because there's anything wrong there, it just happens to throw the exception at that point. The Timestamp type uses DateTime underneath and will therefore return a DateTime value. However, your driver seems to expect a string. If you were to create a user type that will return a string representation of the Timestamp, which seems to be what your driver expects, it may fix your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 5:43 am 
Beginner
Beginner

Joined: Tue Nov 28, 2006 4:26 pm
Posts: 32
Location: Montreal, Quebec, Canada
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 ;).


Top
 Profile  
 
 Post subject: Equivalent of to_char() function in HQL
PostPosted: Fri Jan 05, 2007 10:23 am 
Beginner
Beginner

Joined: Fri Dec 01, 2006 5:16 am
Posts: 23
Location: India
Hi Ivildosola,
Thanks for your reply.But my problem still persists.Actually I am building a Data Access Layer-it has 164 classes with relationships like inheritance and association.I also have one consolidated hbm file which maps all the relationships in the tables.I used a tool called "hbm2net.CodeDOMRenderer" to generate all the C# classes from this hbm file.According to your suggestion(or what I could figure out from your suggestion),I built another class file named "CharTimestampType.cs" where I have housed the code provided by you.

I have also added the tag "<property name="TimestampPropertyName" type="YourNamespace.UserType.CharTimestampType, YourAssemblyName" />" in the hbm file in the following manner

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="com.ml.ps.data">
<property name="TimestampPropertyName" type="com.ml.ps.data.UserType.CharTimestampType, com.ml.ps.data" />


After that I had generated the C# classes using "hbm2net.CodeDOMRenderer" and also compiled the containing assembly.


I have another ASP.Net application which basically uses this assembly and both these projects are in the same solution.


Now when I run the application,I get one peculiar error -"The source file is different from when the module was built"


So, can anybody please advise on this?


Top
 Profile  
 
 Post subject: Equivalent of to_char() function in HQL
PostPosted: Fri Jan 05, 2007 10:26 am 
Beginner
Beginner

Joined: Fri Dec 01, 2006 5:16 am
Posts: 23
Location: India
Hi Ivildosola,
Thanks for your reply.But my problem still persists.Actually I am building a Data Access Layer-it has 164 classes with relationships like inheritance and association.I also have one consolidated hbm file which maps all the relationships in the tables.I used a tool called "hbm2net.CodeDOMRenderer" to generate all the C# classes from this hbm file.According to your suggestion(or what I could figure out from your suggestion),I built another class file named "CharTimestampType.cs" where I have housed the code provided by you.

I have also added the tag "<property name="TimestampPropertyName" type="YourNamespace.UserType.CharTimestampType, YourAssemblyName" />" in the hbm file in the following manner

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="com.ml.ps.data">
<property name="TimestampPropertyName" type="com.ml.ps.data.UserType.CharTimestampType, com.ml.ps.data" />


After that I had generated the C# classes using "hbm2net.CodeDOMRenderer" and also compiled the containing assembly.


I have another ASP.Net application which basically uses this assembly and both these projects are in the same solution.


Now when I run the application,I get one peculiar error -"The source file is different from when the module was built"


So, can anybody please advise on this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 11:36 am 
Beginner
Beginner

Joined: Tue Nov 28, 2006 4:26 pm
Posts: 32
Location: Montreal, Quebec, Canada
I just want to double check to make sure I did not give you incorrect information.

Am I to understand that in your HBM you changed your timestamp properties to something like

Code:
<property name="TimestampPropertyName_1" type="com.ml.ps.data.UserType.CharTimestampType, com.ml.ps.data" />
<property name="TimestampPropertyName_2" type="com.ml.ps.data.UserType.CharTimestampType, com.ml.ps.data" />

Is this correct?


Top
 Profile  
 
 Post subject: Equivalent of to_Char() function in HQL
PostPosted: Mon Jan 08, 2007 3:35 am 
Beginner
Beginner

Joined: Fri Dec 01, 2006 5:16 am
Posts: 23
Location: India
Hi Ivildosola,
Thanks for your reply.Basically I have added below mentioned tag
<property name="TimestampPropertyName" type="com.ml.ps.data.UserType.CharTimestampType, com.ml.ps.data" />
in the following way:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="com.ml.ps.data">
<property name="TimestampPropertyName" type="com.ml.ps.data.UserType.CharTimestampType, com.ml.ps.data" />
<class dynamic-update="true" dynamic-insert="true" table="prime_object" abstract="false" node="PrimeObject" name="com.ml.ps.data.PrimeObject,com.ml.ps.data">


And after that I have replaced the "TimeStamp" word everywhere in the HBM file with "CharTimestampType".


Thanks,

Raul


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 8:50 am 
Beginner
Beginner

Joined: Tue Nov 28, 2006 4:26 pm
Posts: 32
Location: Montreal, Quebec, Canada
I think that may be the source of your problem. If I understand the situation correctly, you may not have modified your HBM properly.

You need to replace every property definition that uses the type TimeStamp with the new type com.ml.ps.data.UserType.CharTimestampType, com.ml.ps.data. For example, if you have the following property definition:

Code:
<property name="p1" type="TimeStamp" />

It needs to be changed to:

Code:
<property name="p1" type="com.ml.ps.data.UserType.CharTimestampType, com.ml.ps.data" />

Let me know if this works. Sorry, but it is difficult to visualize what is causing your problem.


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