-->
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.  [ 12 posts ] 
Author Message
 Post subject: DateTime, and other non-nullable value types
PostPosted: Sat May 14, 2005 9:54 pm 
Regular
Regular

Joined: Fri May 13, 2005 4:08 pm
Posts: 64
I'm a newbie to NHibernate, as many of you know. :wink:

I have an entity defined and all associations and database set up, and it works well, except my DateTime property, which must be set before the entity can be persisted. I guess .NET's DateTime type has a wider range than SQL Server 2000's, so I can see why the DateTime.MinValue may be outside the range of being able to be set during the save, which explains the exception I get when I don't specifically set that property. My question is: what default value of DateTime should I set in my entity class so that if the client doesn't specifically set it, it remains "NULL" in the database?

In other words: what is the equivalent of null in DateTime?

Thanks in advance, and for all previous help I've received on this forum!

P.S. Some of you will probably want to chime in a design ideal of never allowing NULLs in the database. That's fine. I agree -- that it's an ideal. I have to deal with them, so... how is it best done?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 15, 2005 4:04 am 
Newbie

Joined: Fri May 13, 2005 7:46 pm
Posts: 17
I think there's a default-null-value attribute or something you can set for ValueTypes. I just use the Nullables library for NHibernate in the Contrib package though. Works great with a few minor tweaks (I modified the .ToString overloads to match the wrapped classes').


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 15, 2005 4:59 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Right now you can't do anything with it, only use nullables or define your own IUserType for dates and using that. There's no null-value attribute, but it was requested a few times already, so I'll try to think of something for version 1.0. Of course .NET 2 with standard nullable types will make it pretty much unnecessary, but there will always be legacy code.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 8:26 am 
Newbie

Joined: Tue May 17, 2005 3:30 am
Posts: 10
I just ran into the same problem. After reading this and a few other related pages, I tried using the Nullables and Nullables.NHibernate modules that I found elsewhere when I Googled for 'contrib'. Unfortunately, despite taking over an example literally I keep getting a message:

NHibernate.MappingException : could not interpret type: Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate

even though I did add the Nullables and Nullables.NHibernate DLLs in my references.

Does anyone know if this is actually supposed to work?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 8:41 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Yes, users run into this problem sometimes, I haven't been able to find out why it happens. One thing that came into my mind just now is that maybe signing the nullables assemblies causes this, I'll need to check it later.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 2:09 am 
Newbie

Joined: Tue May 31, 2005 11:19 pm
Posts: 11
Location: Brisbane, Australia
Hi,

I've also come across this problem and am really not keen on having to change my fairly sensible DateTime based property mappings file or object data types.

Unless someone comes back to me with a reason why it's a really bad idea, I'm thinking about hacking type\DateTimeType.cs to add the following behaviour on get/set

Get: (if a value in the db is null, return Min Value)
Code:
if (rs[index] is DBNull.Value)
{
   return DateTime.MinValue;
}


Set: (If the DateTime is MinValue, set to Null)
Code:
if (dateValue == DateTime.MinValue)
{
   parm.Value = DBNull.Value;
}
else
{
   parm.Value = new System.Data.SqlTypes.SqlDateTime( dateValue.Year, dateValue.Month, dateValue.Day, dateValue.Hour, dateValue.Minute, dateValue.Second ).Value;
}


If I've understood this correctly this should convert undefined DateTime values (they are automatically set to DateTime.MinValue) to Database nulls and back again.

Of course once c#2.0 there will be nullable DateTime.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 8:31 am 
Newbie

Joined: Thu Jun 09, 2005 5:32 pm
Posts: 2
Dit it work for you?

I was thinking about doing exactly what you said.


Top
 Profile  
 
 Post subject: Works a treat
PostPosted: Mon Jun 20, 2005 8:39 pm 
It worked a treat for me!

I only found this thread when looking for a parallel post, to see if anything had happened there.

Arrived at the same solution independently, good to see I'm not alone in this solution.

Cheers

Simon


Top
  
 
 Post subject: DateTime hack not isolated
PostPosted: Mon Jun 20, 2005 8:42 pm 
It may be of some interest to note that the DateType and TimeType both have this range/null check in their Set overrides too...


Top
  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 2:32 pm 
Newbie

Joined: Fri May 13, 2005 2:13 pm
Posts: 5
Was anyone able to figure out why these exceptions("could not interpret type...") occur?

I need to have the assemblies digitially signed, do you really think that could be the cause? I don't know why that would be though...

I've tried using the full assembly name, but it doesn't work either. Any hints available?


Top
 Profile  
 
 Post subject: Exception
PostPosted: Mon Aug 15, 2005 3:31 pm 
pedro wrote:
Was anyone able to figure out why these exceptions("could not interpret type...") occur?

I need to have the assemblies digitially signed, do you really think that could be the cause? I don't know why that would be though...

I've tried using the full assembly name, but it doesn't work either. Any hints available?

I have.
class TypeFactory() have static constructor static TypeFactory()
this constructor collect all supported types in NHibernate.
and in this collection not present Nullable contrib types.
such us
Code:
         typeByTypeOfName[ typeof( Byte[ ] ).Name ] = NHibernateUtil.Binary;
         typeByTypeOfName[ typeof( Byte[ ] ).AssemblyQualifiedName ] = NHibernateUtil.Binary;
         typeByTypeOfName[ typeof( Boolean ).FullName ] = NHibernateUtil.Boolean;


and method Basic have code
Code:
      public static IType Basic( string name )
      {
         string typeName = String.Empty;

         // Use the basic name (such as String or String(255)) to get the
         // instance of the IType object.
         IType returnType = null;
         returnType = ( IType ) typeByTypeOfName[ name ];
         if( returnType != null )
         {
            return returnType;
         }


and return null (type not found)

Code:
      public static IType HueristicType( string typeName )
      {
         IType type = TypeFactory.Basic( typeName );

return null too
and throw this exception
Code:
            type = TypeFactory.HueristicType( typeNode.Value );
            if (type==null) throw new MappingException("could not interpret type: " + typeNode.Value );


Maby more elastic impliment dymanicaly type loading with special interface implementation?


Top
  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 6:56 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Is there a fix for this? I don't have the luxury of being able to migrate my code to .NET 2.0 at this time ...


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