NHibernate with String Primary Key and Relationships

I just ran into this problem for an hour, and I was annoyingly found the problem in the end.

CIRCUMSTANCES

I have a table in which the user enters a string as a primary key, this table has many relationships from several to one and from many to many of this primary key.

When searching for multiple items from a table, all relationships were returned. However, whenever I tried to get an object using the primary key (string), it did not return any links, they were always 0.

PARTIAL SOLUTION

So, I looked in my logs to see what SQL does and what returned the correct results. Therefore, I tried many different things, and in the end it was. The case of the string passed to the get method was NOT EXACTLY in the same case as in the database, so when he tried to match the elements of the relations with the main entity, he did not find anything (or at least NHIbernate not because as I said above, SQL actually returned the correct results)

REAL SOLUTION

Has anyone else come across this? If so, how do you tell NHibernate to ignore the case of matching SQL results with an entity? This is stupid, because it worked perfectly before suddenly he began to pay attention to the string case.

+5
5

ref . , . , , , NHibernate ISession:

return session.Get<T>(id);

T - , , id - , ( )

:

    <class name="Merchant" table="T__MERCHANT">
        <id name="MerchantId" column="MERCHANT_ID" type="string">
            <generator class="assigned" />
        </id>

        <property name="MerchantStatusId" column="MERCHANT_STATUS_ID" type="Char" not-null="true" length="1" />
        <property name="MerchantStatusName" column="MERCHANT_STATUS_NAME" type="string" length="50" />
        <property name="MerchantName" column="NAME" type="string" not-null="true" length="50" />
 </class>
</hibernate-mapping>

# :

public Merchant GetMerchantById(string id)
{
     return session.Get<Merchant>(id);
}
+4

, , :

<joined-subclass name="JohnHarmanLtd.Web.FineArtCompany.Models.Book, App_Code.tqeub3fb, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">

" App_Code.tqeub3fb". , ASP.NET NHibernate?

- , .

+1

( , DB , IMO).

: 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, , , "" NHibernate.

+1
source

There is no possibility for this, but you can achieve the same using QBE

Example.create(parent).ignoreCase() 
0
source

I do not understand your problem:

  • when you request PK using the Get method, the returned object doesn't matter for the relationship
  • when you request another request (an example will be useful), is the relationship not empty?
0
source

All Articles