-->
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.  [ 4 posts ] 
Author Message
 Post subject: Nullables in NHIB 1.0.4.0 and .NET 2.0
PostPosted: Tue Feb 06, 2007 5:33 pm 
Newbie

Joined: Tue Feb 06, 2007 4:51 pm
Posts: 3
I am trying to use the .Net 2.0 Nullable datetime type with Nhibernate 1.0.4.0, as per details below. I get an invalid cast exception. Am I correct in thinking this should work? The documentation hints that you may be able to
Quote:
Nullables makes it possible to use nullable base types in NHibernate. Note that .NET 2.0 has this feature.


When I use the NullableDateTimeType supplied in the contrib package in my class it works fine.

If I have to use the nullables from contrib, that's fine - but is there a nullable type for SqlDateTime?

Finally, thanks for making a great tool - looking forward to the release of 1.2!

regards,
av



Hibernate version:1.0.4.0

Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="Ecs.App.Informer, Ecs.App" table="Informer">
    <id name="Id" column="InformerId">
      <generator class="assigned" />
    </id>

    <property name="Description" column="Description" />
    <property name="EffectiveFrom" column="EffectiveFrom" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate"/>
    <property name="EffectiveTo" column="EffectiveTo" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate"/>

  </class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
Code:
ISession session = NHibernateSessionManager.Instance.GetSession();

Informer inf = new Informer();

inf.Id = Guid.NewGuid().ToString().Substring(0, 10);
inf.Description = "NUnit Test For Id-" + inf.Id ;
inf.EffectiveFrom = DateTime.Now;
inf.EffectiveTo = null;

session.Save(inf);

NHibernateSessionManager.Instance.GetSession().Flush();


Full stack trace of any exception that occurs:
Code:
NHibernate.ADOException: could not insert: [Ecs.App.Informer#d063306c-e] ---> System.InvalidCastException: Specified cast is not valid.
   at Nullables.NHibernate.NullableDateTimeType.Set(IDbCommand cmd, Object value, Int32 index)
   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session)
   at NHibernate.Persister.EntityPersister.Dehydrate(Object id, Object[] fields, Boolean[] includeProperty, IDbCommand st, ISessionImplementor session)
   at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
   --- End of inner exception stack trace ---
   at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
   at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
   at NHibernate.Impl.ScheduledInsertion.Execute()
   at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
   at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
   at NHibernate.Impl.SessionImpl.Execute()
   at NHibernate.Impl.SessionImpl.Flush()
   at Ecs.App.NUnit.InformersTest.TestCreate() in C:\Dev\Ecs\ECS.App\NUnit\InformersTest.cs:line 46


POCO:
Code:
public class Informer : AuditTaggedBusinessObject<string>
    {
        private DateTime? _effectiveFrom;
        private DateTime? _effectiveTo;
        private string _decription;

        public DateTime? EffectiveFrom
        {
            get
            {
                return _effectiveFrom;
            }
            set
            {
                _effectiveFrom = value;
            }
        }

        public DateTime? EffectiveTo
        {
            get
            {
                return _effectiveTo;
            }
            set
            {
                _effectiveTo = value;
            }
        }

        public bool IsEffective
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }


        /// <summary>
        /// The name of the informer.
        /// </summary>
        public string Description
        {
            get
            {
                return _decription;
            }
            set
            {
                _decription = value;
            }
        }
    }


Name and version of the database you are using:
SQL Server 2005

The generated SQL (show_sql=true):
None generated


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 2:00 am 
Regular
Regular

Joined: Tue Feb 21, 2006 9:50 am
Posts: 107
i think you have to use the NullableDateTime type in your POCO too.

Regards
Klaus


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 5:14 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
If you're using .NET 2.0 you don't need the contrib nullables. If your property is nullable just map it normally and specify the type in the mapping as "DateTime". Works for me.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 9:11 pm 
Newbie

Joined: Tue Feb 06, 2007 4:51 pm
Posts: 3
thanks Symon, that worked for me.


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