Hello all,
I have a custom IUserType setup to handle the following enumeration:
Public Enum Gender As Integer
NotSpecified = 0
Male
Female
End Enum
Note that "NotSpecified" is stored as NULL in my database, thus the
reason for needing a custom IUserType
When I query (using Linq or the Criertia API) on my "NotSpecified"
option, the generated SQL produces a comparison for "= NULL" instead
of "IS NULL" - this obviously results in no rows being returned (under
SQL Server, anyways). I assume this has to do with the fact that I'm
substituting an actual value for a null one - but behind this, I can't
determine any work arounds.
Here is an example of a criteria-based statement that results in
incorrect SQL being generated:
Return Session.CreateCriteria(m_NHEntityType).Add
(NHibernate.Criterion.Restrictions.Eq("GenderId",
Gender.NotSpecified)).List(Of User)()
Here is the implementation of my IUserType:
Imports NHibernate.UserTypes
Imports NHibernate
Imports MyProject.Core.Domain
Namespace NHibernateTypes
Public Class GenderEnumType
Implements IUserType
Public Function DeepCopy(ByVal value As Object) As Object
Implements IUserType.DeepCopy
Return value
End Function
Public Function Replace(ByVal original As Object, ByVal target
As Object, ByVal owner As Object) As Object Implements
IUserType.Replace
Return original
End Function
Public Function Assemble(ByVal cached As Object, ByVal owner
As Object) As Object Implements IUserType.Assemble
Return cached
End Function
Public Function Disassemble(ByVal value As Object) As Object
Implements IUserType.Disassemble
Return value
End Function
Public Shadows Function Equals(ByVal x As Object, ByVal y As
Object) As Boolean Implements IUserType.Equals
If ReferenceEquals(x, y) Then
Return True
End If
If x Is Nothing OrElse y Is Nothing Then
Return False
End If
Return x.Equals(y)
End Function
Public Shadows Function GetHashCode(ByVal x As Object) As
Integer Implements IUserType.GetHashCode
Return If(x Is Nothing, GetType(Gender).GetHashCode() +
473, x.GetHashCode())
End Function
Public ReadOnly Property ReturnedType() As System.Type
Implements NHibernate.UserTypes.IUserType.ReturnedType
Get
Return GetType(Gender)
End Get
End Property
Public ReadOnly Property SqlTypes() As
NHibernate.SqlTypes.SqlType() Implements
NHibernate.UserTypes.IUserType.SqlTypes
Get
Dim types() As NHibernate.SqlTypes.SqlType =
{NHibernateUtil.Int32.SqlType}
Return types
End Get
End Property
Public ReadOnly Property IsMutable() As Boolean Implements
NHibernate.UserTypes.IUserType.IsMutable
Get
Return False
End Get
End Property
Public Overridable Function NullSafeGet(ByVal rs As
IDataReader, ByVal names As String(), ByVal owner As Object) As Object
Implements IUserType.NullSafeGet
Dim value As Integer? = CType(NHibernateUtil.
[Int32].NullSafeGet(rs, names(0)), Integer?)
If value.HasValue Then
Return CType(value, Gender)
Else
Return Gender.NotSpecified
End If
End Function
Public Overridable Sub NullSafeSet(ByVal cmd As IDbCommand,
ByVal value As Object, ByVal index As Integer) Implements
IUserType.NullSafeSet
Dim enumValue As Integer = CType(value, Gender)
If enumValue <> Gender.NotSpecified Then
DirectCast(cmd.Parameters(index),
IDataParameter).Value = enumValue
Else
DirectCast(cmd.Parameters(index),
IDataParameter).Value = DBNull.Value
End If
End Sub
End Class
End Namespace
Any suggestions on how I can get around this?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---