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.  [ 3 posts ] 
Author Message
 Post subject: [Beginner] Nullables Casting Problem
PostPosted: Mon Aug 07, 2006 12:21 pm 
Newbie

Joined: Mon Aug 07, 2006 11:50 am
Posts: 2
I am trying to insert some basic information into a table called location. I am using Oracle and allowing null data, so I am using the datatypes found in Nullables.NHibernate. I am having a casting problem between the nullables' datatypes and normal .Net datatypes.

For instance, I am declaring an addDate variable as a NullableDateTimeType, but when I attempt to assign a value to it (I.E. addDate = New System.DateTime(1979, 11, 8)) I receive an InvalidCastException Exception.

I have tried searching through these forums and google, but have been unable to find anything similiar to my problem.

Thank you.

Oracle table:

Code:
CREATE TABLE LOCATION
(
  LOCATIONID    INTEGER,
  NAME          VARCHAR2(255 CHAR),
  DESCRIPTION   CLOB,
  ADDRESS1      VARCHAR2(255 CHAR),
  ADDRESS2      VARCHAR2(255 CHAR),
  CITY          VARCHAR2(255 CHAR),
  STATE         VARCHAR2(2 CHAR),
  ZIP           VARCHAR2(20 CHAR),
  ADDDATE       DATE,
  MODIFIEDDATE  DATE,
  ADDEDBY       INTEGER,
  MODIFIEDBY    INTEGER
)


My mapping file looks like

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="test" namespace="test">
    <class name="LocationModel" table="Location">
        <id name="locationID" type="Int32" length="20">
            <generator class="assigned" />
        </id>
        <property name="name" type="Nullables.NHibernate.EmptyStringType, Nullables.NHibernate" length="255" not-null="false"></property>
        <property name="description" type="Nullables.NHibernate.EmptyStringType, Nullables.NHibernate" length="8000" not-null="false"></property>
        <property name="address1" type="Nullables.NHibernate.EmptyStringType, Nullables.NHibernate" length="255" not-null="false"></property>
        <property name="address2" type="Nullables.NHibernate.EmptyStringType, Nullables.NHibernate" length="255" not-null="false"></property>
        <property name="state" type="Nullables.NHibernate.EmptyStringType, Nullables.NHibernate" length="2" not-null="false"></property>
        <property name="city" type="Nullables.NHibernate.EmptyStringType, Nullables.NHibernate" length="255" not-null="false"></property>
        <property name="zip" type="Nullables.NHibernate.EmptyStringType, Nullables.NHibernate" length="20" not-null="false"></property>
        <property name="addDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" not-null="false"></property>
        <property name="modifiedDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" not-null="false"></property>
      <one-to-one name="addedBy" class="SecurityModel" />
      <one-to-one name="modifiedBy" class="SecurityModel" />      
    </class>
</hibernate-mapping>


My Class looks like
Code:
Imports Nullables
Imports Nullables.NHibernate

Public Class LocationModel
    Private _locationID As Int32
    Private _name As EmptyStringType
    Private _description As EmptyStringType
    Private _address1 As EmptyStringType
    Private _address2 As EmptyStringType
    Private _state As EmptyStringType
    Private _city As EmptyStringType
    Private _zip As EmptyStringType
    Private _addDate As NullableDateTimeType
    Private _modifiedDate As NullableDateTimeType
    Private _addedBy As IDictionary
    Private _modifiedBy As IDictionary
    Private _active As Boolean


    Public Overridable Property locationID()
    Get
        Return _locationID
    End Get
    Set(ByVal Value)
        _locationID = Value
    End Set
    End Property
    Public Overridable Property name()
    Get
        Return _name
    End Get
    Set(ByVal Value)
        _name = Value
    End Set
    End Property
    Public Overridable Property description()
    Get
        Return _description
    End Get
    Set(ByVal Value)
        _description = Value
    End Set
    End Property
    Public Overridable Property address1()
    Get
        Return _address1
    End Get
    Set(ByVal Value)
        _address1 = Value
    End Set
    End Property
    Public Overridable Property address2()
    Get
        Return _address2
    End Get
    Set(ByVal Value)
        _address2 = Value
    End Set
    End Property
    Public Overridable Property city()
    Get
        Return _city
    End Get
    Set(ByVal Value)
        _city = Value
    End Set
    End Property
    Public Overridable Property state()
    Get
        Return _state
    End Get
    Set(ByVal Value)
        _state = Value
    End Set
    End Property
    Public Overridable Property zip()
    Get
        Return _zip
    End Get
    Set(ByVal Value)
        _zip = Value
    End Set
    End Property
    Public Overridable Property addDate()
    Get
        Return _addDate
    End Get
    Set(ByVal Value)
        _addDate = Value
    End Set
    End Property
    Public Overridable Property modifiedDate()
    Get
        Return _modifiedDate
    End Get
    Set(ByVal Value)
        _modifiedDate = Value
    End Set
    End Property
    Public Overridable Property addedBy()
    Get
        Return _addedBy
    End Get
    Set(ByVal Value)
        _addedBy = Value
    End Set
    End Property
    Public Overridable Property modifiedBy()
    Get
        Return _modifiedBy
    End Get
    Set(ByVal Value)
        _modifiedBy = Value
    End Set
    End Property
    Public Overridable Property active()
    Get
        Return _active
    End Get
    Set(ByVal Value)
        _active = Value
    End Set
    End Property

    Public Sub New()
        _locationID = 0
        _name = New EmptyStringType
        _description = New EmptyStringType
        _address1 = New EmptyStringType
        _address2 = New EmptyStringType
        _state = New EmptyStringType
        _city = New EmptyStringType
        _zip = New EmptyStringType
        _addDate = New NullableDateTimeType
        _modifiedDate = New NullableDateTimeType
        _addedBy = Nothing
        _modifiedBy = Nothing
        _active = True
    End Sub
End Class



Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 3:55 pm 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
in mappings use NullableDateTimeType, but in your code use NullableDateTime from the Nullables assembly. The same applies for all nullables.
By the way, for strings there is no need to use NullableStringType, the plain string works. You need nullables only for value typed objects.

cheers,
radu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 4:22 pm 
Newbie

Joined: Mon Aug 07, 2006 11:50 am
Posts: 2
Thanks Radu.

I converted my class to use Strings instead of EmptyStringType, and I used NullableDateTime instead of NullableDateTimeType. My insert and selects appear to be working properly now. Thanks for your help.


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