Skip to content

Request for Clarification on Composite Key and Dynamic Model Mapping in NHibernate #3633

@Ganesh13024

Description

@Ganesh13024

Dear NHibernate Team,

I am reaching out to discuss an issue related to composite keys and dynamic model mapping that I recently mentioned in the NHUsers forum. To provide better context, I would like to include my question along with some test case results here.

Could you please clarify the concerns I raised in the thread? Additionally, I would like to know if the suggested changes might introduce any complexities in NHibernate's execution flow. Notably, after implementing the changes, I am obtaining the correct output.

Looking forward to your insights and guidance.

As I mentioned earlier, this is my mapper code:

<?xml version="1.0" encoding="utf-16"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class entity-name="Mycustomtable" table="Mycustomtable">
    <composite-id>
      <key-property name="TenantId" column="tenantid" type="int" />
      <key-property name="ObjectId" column="objectid" type="int" />
    </composite-id>
    <property name="Name" column="Name" type="string" />
    <property name="Email" column="Email" type="string" />
    <property name="PhoneNumber" column="PhoneNumber" type="string" />
    <property name="StreetAddress" column="StreetAddress" type="string" />
    <property name="City" column="City" type="string" />
    <property name="State" column="State" type="string" />
    <property name="PostalCode" column="PostalCode" type="string" />
    <property name="Country" column="Country" type="string" />
    <property name="Company" column="Company" type="string" />
    <property name="Industry" column="Industry" type="string" />
    <property name="Role" column="Role" type="string" />
    <property name="TaxId" column="TaxId" type="string" />
    <property name="IsCompliant" column="IsCompliant" type="bool" />
    <property name="AccountManagerId" column="AccountManagerId" type="int" />
    <property name="CustomerSince" column="CustomerSince" type="DateTime" />
    <property name="LastContactDate" column="LastContactDate" type="DateTime" />
    <property name="CreditLimit" column="CreditLimit" type="decimal" />
    <property name="BalanceDue" column="BalanceDue" type="decimal" />
    <property name="Notes" column="Notes" type="string" />
    <property name="IsActive" column="IsActive" type="bool" />
  </class>
</hibernate-mapping>

This is my sample criteria:

var criteria = session.CreateCriteria("Mycustomtable")
    .Add(Restrictions.Eq("TenantId", 123))
    .Add(Restrictions.Eq("ObjectId", 456))
    .Add(Restrictions.Eq("IsActive", true))
    .Add(Restrictions.Like("Name", "%John%"))
    .Add(Restrictions.Between("CustomerSince", new DateTime(2020, 1, 1), DateTime.Now))
    .Add(
        Restrictions.Or(
            Restrictions.Eq("Country", "USA"),
            Restrictions.Eq("Country", "Canada")
        )
    )
    .Add(
        Restrictions.And(
            Restrictions.Ge("CreditLimit", 5000m),
            Restrictions.Le("BalanceDue", 1000m)
        )
    )
    .Add(Restrictions.IsNotNull("Email"))
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.GroupProperty("Country"), "Country")
        .Add(Projections.RowCount(), "RowCount")
        .Add(Projections.Max("CreditLimit"), "MaxCreditLimit")
        .Add(Projections.Sum("BalanceDue"), "TotalBalanceDue")
    )
    .AddOrder(Order.Desc("RowCount"))
    .AddOrder(Order.Asc("Country"))
    .SetResultTransformer(NHibernate.Transform.Transformers.AliasToEntityMap);

var results = criteria.List();

Sample data for the criteria:

image

The exception from the existing implementation is:

NHibernate.QueryException
HResult=0x80131500
Message=could not resolve property: TenantId of: Mycustomtable
Source=NHibernate
StackTrace:
   at NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(String propertyName) 
   in C:\Users\GaneshkumarM\source\repos\nhibernate-core\src\NHibernate\Persister\Entity\AbstractPropertyMapping.cs:line 37
   ...

Best regards,
Ganeshkumar M

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions