これが私の回避策です(これは回避策です。IMOで設計が不十分なインプレースDBを扱っています)。
元のソース: https://forum.hibernate.org/viewtopic.php?f=25&t=979685&view=previous
これが私のカスタム文字列クラスです(VBで):
Imports NHibernate.UserTypes
Imports NHibernate.Type
Imports NHibernate.SqlTypes
Public Class CaseInsensitiveStringType
Inherits AbstractStringType
' Methods
Public Sub New()
MyBase.New(New StringSqlType)
End Sub
Public Sub New(ByVal sqlType As StringSqlType)
MyBase.New(sqlType)
End Sub
' Properties
Public Overrides ReadOnly Property Name As String
Get
Return "InsensitiveString"
End Get
End Property
Public Overrides Function IsEqual(ByVal x As Object, ByVal y As Object) As Boolean
Return MyBase.IsEqual(x, y) OrElse (x IsNot Nothing AndAlso y IsNot Nothing AndAlso String.Equals(x, y, StringComparison.InvariantCultureIgnoreCase))
End Function
Public Overrides Function GetHashCode(ByVal x As Object, ByVal entityMode As NHibernate.EntityMode, ByVal factory As NHibernate.Engine.ISessionFactoryImplementor) As Integer
Return MyBase.GetHashCode(x.ToString().Trim().ToUpperInvariant(), entityMode, factory)
End Function
End Class
私は FluentNhibernate を使用しているので、マッピングは次のようになります (Id フィールドにのみ適用されます)。
Id(Function(x) x.Id).GeneratedBy.Assigned().CustomType(Of CaseInsensitiveStringType)()
そして、それはそれでした。私の関連付けは再び適切に取り込まれ始めました。
繰り返しますが、文字列ではなく int/GUID PK を使用して DB を正しく設計する方がはるかに優れていますが、必要な場合、これは NHibernate を「修正」する適切な方法です。