Nhibernate Duplicate data is result is coming,mapping issue - nhibernate

I am stuck with an issue in nHibernate.I have a view which has multiple records in the table corresponding to the one notification id.For example-for notification id=1 multiple records are there:
Below is my n hibernate mapping class.It inherits from Emailnotification base.which has all the base properties
Below is my domain class for the view:
public partial class vwNotificationPolicyDeliveryCoverLetters : EmailNotificationBase
{
public vwNotificationPolicyDeliveryCoverLetters()
{
//ReasonOfInAccuracy = new List<vwNotificationInaccuracyFailureReasons>();
EmailNotificationBase = new List<EmailNotificationBase>();
}
public virtual long NotificationId
{
get;
set;
}
public virtual int? PolicyAccurateLookupId
{
get;
set;
}
public virtual string PolicyCheckBy
{
get;
set;
}
public virtual string SendOnBehalfOf
{
get;
set;
}
public virtual string InAccuracyComments
{
get;
set;
}
public virtual DateTime? PolicyCheckDate
{
get;
set;
}
public virtual string ReasonOfInAccuracy
{
get;
set;
}
public virtual string PolicyNumber
{
get;
set;
}
public virtual string PolicyLobCode
{
get;
set;
}
public virtual string PolicyDescription
{
get;
set;
}
public virtual string PolicyType
{
get;
set;
}
public virtual DateTime? PolicyEffectiveDate
{
get;
set;
}
public virtual double? PolicyId
{
get;
set;
}
public virtual string ContactName
{
get;
set;
}
public virtual string DeliveryContactEmail
{
get;
set;
}
public virtual string SendOnBehalfOfEmailAddress
{
get;
set;
}
public virtual long DeliveryContactId
{
get;
set;
}
public virtual DateTime? PolicyExpirationDate
{
get;
set;
}
public virtual IList<EmailNotificationBase> EmailNotificationBase
{
get;
set;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
return true;
return Equals(obj as vwNotificationPolicyDeliveryCoverLetters);
}
public virtual bool Equals(vwNotificationPolicyDeliveryCoverLetters obj)
{
if (obj == null) return false;
if (Equals(Action, obj.Action) == false) return false;
if (Equals(Comments, obj.Comments) == false) return false;
if (Equals(Link, obj.Link) == false) return false;
if (Equals(NotificationId, obj.NotificationId) == false) return false;
if (Equals(NotificationIndicator, obj.NotificationIndicator) == false) return false;
if (Equals(NotificationStatus, obj.NotificationStatus) == false) return false;
if (Equals(NotificationType, obj.NotificationType) == false) return false;
if (Equals(RequestState, obj.RequestState) == false) return false;
if (Equals(ServiceRequestId, obj.ServiceRequestId) == false) return false;
if (Equals(ServiceRequestNumber, obj.ServiceRequestNumber) == false) return false;
if (Equals(ClientName, obj.ClientName) == false) return false;
if (Equals(ClientNumber, obj.ClientNumber) == false) return false;
if (Equals(PolicyId, obj.PolicyId) == false) return false;
if (Equals(PolicyNumber, obj.PolicyNumber) == false) return false;
if (Equals(PolicyLobCode, obj.PolicyLobCode) == false) return false;
if (Equals(PolicyDescription, obj.PolicyDescription) == false) return false;
if (Equals(PolicyType, obj.PolicyType) == false) return false;
if (Equals(ServicingLocation, obj.ServicingLocation) == false) return false;
if (Equals(PolicyEffectiveDate, obj.PolicyEffectiveDate) == false) return false;
if (Equals(ContactName, obj.ContactName) == false) return false;
if (Equals(DeliveryContactEmail, obj.DeliveryContactEmail) == false) return false;
if (Equals(SendOnBehalfOfEmailAddress, obj.SendOnBehalfOfEmailAddress) == false) return false;
if (Equals(DeliveryContactId, obj.DeliveryContactId) == false) return false;
if (Equals(PolicyExpirationDate, obj.PolicyExpirationDate) == false) return false;
if (Equals(PolicyAccurateLookupId, obj.PolicyAccurateLookupId) == false) return false;
if (Equals(PolicyCheckBy, obj.PolicyCheckBy) == false) return false;
if (Equals(PolicyCheckDate, obj.PolicyCheckDate) == false) return false;
if (Equals(ReasonOfInAccuracy, obj.ReasonOfInAccuracy) == false) return false;
if (Equals(InAccuracyComments, obj.InAccuracyComments) == false) return false;
if (Equals(SendOnBehalfOf, obj.SendOnBehalfOf) == false) return false;
return true;
}
public override int GetHashCode()
{
int result = 1;
result = (result * 397) ^ (Action != null ? Action.GetHashCode() : 0);
result = (result * 397) ^ (Comments != null ? Comments.GetHashCode() : 0);
result = (result * 397) ^ (Link != null ? Link.GetHashCode() : 0);
result = (result * 397) ^ NotificationId.GetHashCode();
result = (result * 397) ^ (NotificationIndicator != null ? NotificationIndicator.GetHashCode() : 0);
result = (result * 397) ^ (NotificationStatus != null ? NotificationStatus.GetHashCode() : 0);
result = (result * 397) ^ (NotificationType != null ? NotificationType.GetHashCode() : 0);
result = (result * 397) ^ (RequestState != null ? RequestState.GetHashCode() : 0);
result = (result * 397) ^ (ServiceRequestId != null ? ServiceRequestId.GetHashCode() : 0);
result = (result * 397) ^ (ServiceRequestNumber != null ? ServiceRequestNumber.GetHashCode() : 0);
result = (result * 397) ^ (ClientName != null ? ClientName.GetHashCode() : 0);
result = (result * 397) ^ (ClientNumber != null ? ClientNumber.GetHashCode() : 0);
result = (result * 397) ^ (PolicyId != null ? PolicyId.GetHashCode() : 0);
result = (result * 397) ^ (PolicyNumber != null ? PolicyNumber.GetHashCode() : 0);
result = (result * 397) ^ (PolicyLobCode != null ? PolicyLobCode.GetHashCode() : 0);
result = (result * 397) ^ (PolicyDescription != null ? PolicyDescription.GetHashCode() : 0);
result = (result * 397) ^ (PolicyType != null ? PolicyType.GetHashCode() : 0);
result = (result * 397) ^ (ServicingLocation != null ? ServicingLocation.GetHashCode() : 0);
result = (result * 397) ^ (PolicyEffectiveDate != null ? PolicyEffectiveDate.GetHashCode() : 0);
result = (result * 397) ^ (ContactName != null ? ContactName.GetHashCode() : 0);
result = (result * 397) ^ (DeliveryContactEmail != null ? DeliveryContactEmail.GetHashCode() : 0);
result = (result * 397) ^ (SendOnBehalfOfEmailAddress != null ? SendOnBehalfOfEmailAddress.GetHashCode() : 0);
result = (result * 397) ^ DeliveryContactId.GetHashCode();
result = (result * 397) ^ (PolicyAccurateLookupId != null ? PolicyAccurateLookupId.GetHashCode() : 0);
result = (result * 397) ^ (PolicyCheckBy != null ? PolicyCheckBy.GetHashCode() : 0);
result = (result * 397) ^ (PolicyCheckDate != null ? PolicyCheckDate.GetHashCode() : 0);
result = (result * 397) ^ (ReasonOfInAccuracy != null ? ReasonOfInAccuracy.GetHashCode() : 0);
result = (result * 397) ^ (PolicyExpirationDate != null ? PolicyExpirationDate.GetHashCode() : 0);
result = (result * 397) ^ (InAccuracyComments != null ? InAccuracyComments.GetHashCode() : 0);
result = (result * 397) ^ (SendOnBehalfOf != null ? SendOnBehalfOf.GetHashCode() : 0);
return result;
}
}
}
Below is the hbm file for the base class.Inside that union-sub class has been created for the child class:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Aon.ATD.Domain.Model" assembly="Aon.ATD.DomainModel" xmlns="urn:nhibernate-mapping-2.2">
<class name="EmailNotificationBase" abstract="true">
<id name="NotificationId" access="property" column="`NotificationId`">
<generator class="assigned" />
</id>
<property name="ServiceRequestId" column="`ServiceRequestId`" />
<property name="ServiceRequestNumber" column="`ServiceRequestNumber`" />
<property name="ClientName" column="`ClientName`" />
<property name="ClientNumber" column="`ClientNumber`" />
<property name="AttachmentGroupId" column="`AttachmentGroupId`" />
<property name="Comments" column="`Comments`" />
<property name="NotificationType" column="`NotificationType`" />
<property name="NotificationStatus" column="`NotificationStatus`" />
<property name="Action" column="`Action`" />
<property name="Link" column="`Link`" />
<property name="NotificationIndicator" column="`Notification Indicator`" />
<property name="RequestState" column="`RequestState`" />
<property name="NotificationTypeId" column="`NotificationTypeId`" />
<property name="DistributionTypeId" column="`DistributionTypeId`" />
<property name="DistributionType" column="`DistributionType`" />
<property name="Office" column="`Office`" />
<property name="ModifiedDateTime" column="`ModifiedDateTime`" />
<property name="ServicingLocation" column="`ServicingLocation`" />
<property name="RequestCategoryId" column="`RequestCategoryId`" />
<property name="RequestTypeId" column="`RequestTypeId`" />
<many-to-one name="AttachmentGroup" class="AttachmentGroup" column="`AttachmentGroupId`" not-null="false" fetch="select" lazy="false" />
<!--<bag name="PolicyDeliveryCoverLetters" inverse="true" >
<key column="`NotificationId`" />
<one-to-many class="vwNotificationPolicyDeliveryCoverLetters" />
</bag>-->
<union-subclass name="VwEmailNotification" table="`VwEmailNotification`" />
<union-subclass name="VwNotificationAudit" table="`vwNotificationAudit`">
<property name="PolicyNumber" column="`PolicyNumber`" />
<property name="LobCode" column="`LobCode`" />
<property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
<property name="PolicyId" column="`PolicyId`" />
<property name="CarrierName" column="`CarrierName`" />
<property name="DocumentType" column="`DocumentType`" />
<!--
<bag name="PITDSRsByPolicyID" inverse="true" lazy="false">
<key column="`PolicyId`" />
<one-to-many class="VwPITDSRByPolicyId" />
</bag>
-->
</union-subclass>
<union-subclass name="VwNotificationEndorsement" table="`vwNotificationEndorsement`">
<property name="PolicyNumber" column="`PolicyNumber`" />
<property name="LobCode" column="`LobCode`" />
<property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
<property name="PolicyId" column="`PolicyId`" />
<property name="CarrierName" column="`CarrierName`" />
</union-subclass>
<union-subclass name="VwNotificationPolicy" table="`vwNotificationPolicy`">
<property name="PolicyNumber" column="`PolicyNumber`" />
<property name="LobCode" column="`LobCode`" />
<property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
<property name="PolicyId" column="`PolicyId`" />
<property name="CarrierName" column="`CarrierName`" />
<property name="DocumentType" column="`DocumentType`" />
<!--
<bag name="PITDSRsByPolicyID" inverse="true" lazy="false">
<key column="`PolicyId`" />
<one-to-many class="VwPITDSRByPolicyId" />
</bag>
-->
</union-subclass>
<union-subclass name="VwNotificationNotARSAccount" table="`vwNotificationNotARSAccount`">
<property name="PolicyNumber" column="`PolicyNumber`" />
<property name="LobCode" column="`LobCode`" />
<property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
<property name="PolicyId" column="`PolicyID`" />
<property name="CarrierName" column="`CarrierName`" />
<property name="EmailAddress" column="`EmailAddress`" />
</union-subclass>
<union-subclass name="VwNotificationOfficePend" table="`vwNotificationOfficePend`">
<property name="PolicyNumber" column="`PolicyNumber`" />
<property name="LobCode" column="`LobCode`" />
<property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
<property name="EmailAddress" column="`EmailAddress`" />
</union-subclass>
<union-subclass name="VwNotificationNotAONServiced" table="`vwNotificationNotAONServiced`">
<property name="PolicyNumber" column="`PolicyNumber`" />
<property name="LobCode" column="`LobCode`" />
<property name="CarrierName" column="`CarrierName`" />
<property name="CarrierGroupCode" column="`CarrierGroupCode`" />
</union-subclass>
<union-subclass name="VwNotificationDocumentOutOfScope" table="`vwNotificationDocumentOutOfScope`">
<property name="PolicyNumber" column="`PolicyNumber`" />
<property name="LobCode" column="`LobCode`" />
<property name="CarrierName" column="`CarrierName`" />
<property name="DocumentType" column="`DocumentType`" />
<property name="CarrierGroupCode" column="`CarrierGroupCode`" />
</union-subclass>
<union-subclass name="VwNotificationDiscrepancy" table="`vwNotificationDiscrepancy`">
<property name="Astindicator" column="`Astindicator`" />
<property name="PolicyId" column="`PolicyId`" />
</union-subclass>
<union-subclass name="VwNotificationPolicyDelivery" table="`vwNotificationPolicyDelivery`">
<property name="PolicyNumber" column="`PolicyNumber`" />
<property name="PolicyLobCode" column="`PolicyLobCode`" />
<property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
<property name="PolicyId" column="`PolicyID`" />
<property name="PolicyDescription" column="`PolicyDescription`" />
<property name="PolicyType" column="`PolicyType`" />
</union-subclass>
<union-subclass name="vwNotificationPolicyDeliveryCoverLetters" table="`vwNotificationPolicyDeliveryCoverLetters`">
<property name="PolicyNumber" column="`PolicyNumber`" />
<property name="PolicyLobCode" column="`PolicyLobCode`" />
<property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
<property name="PolicyId" column="`PolicyID`" />
<property name="PolicyDescription" column="`PolicyDescription`" />
<property name="PolicyType" column="`PolicyType`" />
<property name="ContactName" column="`ContactName`" />
<property name="DeliveryContactEmail" column="`DeliveryContactEmail`" />
<property name="SendOnBehalfOfEmailAddress" column="`SendOnBehalfOfEmailAddress`" />
<property name="DeliveryContactId" column="`DeliveryContactId`" />
<property name="PolicyExpirationDate" column="`PolicyExpirationDate`" />
<property name="PolicyAccurateLookupId" type="Int32" column="`PolicyAccurateLookupId`" />
<property name="PolicyCheckBy" type="String" column="`PolicyCheckBy`" />
<property name="PolicyCheckDate" type="DateTime" column="`PolicyCheckDate`" />
<property name="ReasonOfInAccuracy" column="`ReasonOfInAccuracy`" />
<property name="InAccuracyComments" column="`InAccuracyComments`" />
<property name="SendOnBehalfOf" column="`SendOnBehalfOf`" />
<property name="NotificationId" column="`NotificationId`" />
</union-subclass>
While fetching the records first row corresponding to the notification id is getting duplicated.For example for notification id=1 there are three rows.While fetching the records 2nd and 3rd row is having the same data as first row.

This is expected behavior. NHibernate sees that second and third record have same id so the data must be the same (id = identity). It just hands out the reference to the already created first object.
What you really have here is a one-to-many association of value objects instead of a union subclass, so model it as that:
class vwNotificationPolicyDeliveryCoverLetters : EmailNotificationBase
{
public virtual Notification Owner { get; set; }
//other properties
}
<class name="VwEmailNotification" table="`VwEmailNotification`" />
<composite-id>
<key-many-to-one name="Owner" column="`notification_id`" />
<key-property name="PolicyNumber" column="`PolicyNumber`" />
<key-property name="LobCode" column="`LobCode`" />
<key-property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
<key-property name="PolicyId" column="`PolicyId`" />
<key-property name="CarrierName" column="`CarrierName`" />
<key-property name="DocumentType" column="`DocumentType`" />
</composite-id>
</class>
the compositeid over all properties matches your equal implementation

Related

NHibernate.PropertyAccessException was unhandled

I am new to NHibernate. Just started learning NHibernate.
I am getting for run-time error with my C# code
NHibernate.PropertyAccessException was unhandled
Message=Invalid Cast (check your mapping for property type mismatches); setter of NHibernateDemo.Customer
InnerException: System.InvalidCastException
Message=Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericSet`1[NHibernateDemo.Order]' to type 'System.Collections.Generic.ISet`1[NHibernateDemo.Order]'.
Following is my C# code written for
public class Customer {
public Customer()
{
MemberSince = DateTime.UtcNow;
Orders = new HashSet<Order>();
}
public virtual Guid Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual Location Address { get; set; }
public virtual ISet<Order> Orders { get; set; }
}
It's hbm file is as follow
<class name="Customer" table="Customer" lazy="true" >
<id name="Id">
<generator class="guid.comb" />
</id>
<property name="FirstName" />
<property name="LastName" />
<component name="Address" >
<property name="Street" />
<property name="City" />
<property name="State" />
<property name="Country" />
</component>
<set name="Orders" table="`Order`" order-by="Ordered desc">
<key column="CustomerId" />
<one-to-many class="Order" />
</set>
</class>
public class Order {
public virtual Guid Id { get; set; }
public virtual DateTime Ordered { get; set; }
public virtual DateTime Shipped { get; set; }
public virtual Location ShipAddress { get; set; }
public virtual Customer Customer { get; set; }
}
<class name="Order" table="`Order`">
<id name="Id">
<generator class="guid.comb" />
</id>
<property name="Ordered" />
<property name="Shipped" />
<component name="ShipAddress" >
<property name="Street" />
<property name="City" />
<property name="State" />
<property name="Country" />
</component>
<many-to-one name="Customer" column="CustomerId" />
</class>
If I change "set" section to "list" in Customer HBM file and do necessary changes in Customer class. Program is running correctly. Also if i remove "set" section from Customer HBM file; it is working.
Can you please help me to find what is wrong with "set" section of Customer HBM file?
Your mapping is almost correct, but the ISet interface is not from System namespace but from iesi library (distributed with NHibernate)
So you can reference iesi and change your mapping:
public virtual Iesi.Collections.Generic.ISet<Order> Orders { get; set; }
Or use the IList<>
and mapping with a bag
<bag name="Orders" table="`Order`" order-by="Ordered desc">
<key column="CustomerId" />
<one-to-many class="Order" />
</bag>
NOTE also do not forget to init the list, just in case that entity is created via new operator and NOT loaded by NHibernate

NHibernate Could not determine type for [namespace,assembly] for columns: NHibernate.Mapping.Column(column)

I'm confused with this situation. Having these two classes:
public class Payment {
public Payment() { }
public string Trn { get; set; }
public TxType TxTypeId { get; set; }
public string TxCode { get; set; }
public System.Nullable<decimal> Amount { get; set; }
public System.Nullable<System.DateTime> DateStamp { get; set; } }
public PaymentAudit() { }
public System.DateTime DateStamp { get; set; }
public Payment Trn { get; set; }
public PaymentSaga PaymentStateId { get; set; }
public ProcessState ProcessState { get; set; }
public PublishState PublishState { get; set; }
public System.Nullable<short> ChgCount { get; set; }
public string UserName { get; set; }
And mappings are:
<class name="Payment" table="Payment" lazy="false" >
<id name="Trn">
<generator class="identity" />
</id>
<many-to-one insert="false" update="false" lazy="false" name="TxTypeId">
<column name="TxTypeId" sql-type="varchar" not-null="false" />
</many-to-one>
<property name="TxTypeId">
<column name="TxTypeId" sql-type="varchar" not-null="false" />
</property>
<many-to-one insert="false" update="false" lazy="false" name="TxCode">
<column name="TxCode" sql-type="varchar" not-null="false" />
</many-to-one>
<property name="TxCode">
<column name="TxCode" sql-type="varchar" not-null="false" />
</property>
<property name="Amount">
<column name="Amount" sql-type="decimal" not-null="false" />
</property>
<property name="DateStamp">
<column name="DateStamp" sql-type="datetime" not-null="false" />
</property>
<bag name="PaymentAudits" inverse="true" cascade="none">
<key column="Trn" />
<one-to-many class="PaymentAudit" not-found="ignore" />
</bag>
</class>
<class name="PaymentAudit" table="PaymentAudit" >
<composite-id>
<key-many-to-one name="PaymentStateId" column="PaymentStateId" />
<key-property name="DateStamp" column="DateStamp" />
<key-many-to-one name="ProcessState" column="ProcessState" />
</composite-id>
<property name="PublishState">
<column name="PublishState" sql-type="varchar" not-null="false" />
</property>
<many-to-one name="Trn">
<column name="Trn" sql-type="varchar" not-null="false" />
</many-to-one>
<property name="ChgCount">
<column name="ChgCount" sql-type="smallint" not-null="false" />
</property>
<property name="UserName">
<column name="UserName" sql-type="nvarchar" not-null="false" />
</property>
<many-to-one name="PublishState">
<column name="PublishState" sql-type="varchar" not-null="false" />
</many-to-one>
</class>
The tables,
Payment:
Trn PK
TxTypeId FK
TxCode FK
Amount
DateStamp
PaymentAudit:
PaymentStateId PK
DateStamp PK
ProcessState PK
PublishState FK
Trn FK
ChgCount
UserName
And I got this error:
Could not determine type for: MyProject.NHibernate.Payment.Model.Payment.Payment, MyProject.NHibernate.Payment.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(Trn)
Any ideas?
Thanks.
Trn is the primary key, is a string and is mapped as identity? Identity is an integer, so the property should be int.

Nhibernate: Foreign key must have same number of columns as the referenced primary key

I'm confused with this situation. Having these two classes:
public class TxType {
public TxType()
{
Payments = new List<Payment>();
}
public string TxTypeId { get; set; }
public string TxCode { get; set; }
public IList<Payment> Payments { get; set; }
public string Description { get; set; } }
public class Payment {
public Payment() { }
public string Trn { get; set; }
public TxType TxTypeId { get; set; }
public string TxCode { get; set; }
public System.Nullable<decimal> Amount { get; set; }
public System.Nullable<System.DateTime> DateStamp { get; set; } }
TxType table have two PK (TxTypeId, TxCode), and both are FK in Payment Table.
These are my mappings:
<class name="TxType" table="TxType" lazy="false" >
<composite-id>
<key-property name="TxTypeId" column="TxTypeId" />
<key-property name="TxCode" column="TxCode" />
</composite-id>
<property name="Description">
<column name="Description" sql-type="nvarchar" not-null="false" />
</property>
<bag name="Payments" inverse="true" cascade="none">
<key>
<column name="TxTypeId" />
<column name="TxCode" />
</key>
<one-to-many class="MyProject.Nhibernate.Repository.Payment" not-found="ignore" />
</bag>
</class>
<class name="Payment" table="Payment" lazy="false" >
<id name="Trn">
<generator class="identity" />
</id>
<many-to-one insert="false" update="false" lazy="false" name="TxTypeId">
<column name="TxTypeId" sql-type="varchar" not-null="false" />
</many-to-one>
<property name="TxTypeId">
<column name="TxTypeId" sql-type="varchar" not-null="false" />
</property>
<many-to-one insert="false" update="false" lazy="false" name="TxCode">
<column name="TxCode" sql-type="varchar" not-null="false" />
</many-to-one>
<property name="TxCode">
<column name="TxCode" sql-type="varchar" not-null="false" />
</property>
<property name="Amount">
<column name="Amount" sql-type="decimal" not-null="false" />
</property>
<property name="DateStamp">
<column name="DateStamp" sql-type="datetime" not-null="false" />
</property>
</class>
And I'm stuck with this error:
Foreign key (FKF4FA0208CDBA724F:Payment [TxTypeId])) must have same number of columns as the referenced primary key (TxType [TxTypeId, TxCode]).
Thanks!
Okay, i figured out by adding these lines in Payment mapping:
<many-to-one name="TxType" class="MyProject.Nhibernate.Repository.TxType" insert="false" update="false" lazy="false" >
<column name="TxTypeId" sql-type="varchar" not-null="false" />
<column name="TxCode" sql-type="varchar" not-null="false" />
</many-to-one>
Thanks!
Your id property must be only one. That is, if your table has a composite primary key, you should create a class for mapping to these columns. Remember, the Get/Load methods only take a single parameter for the id, not many.

how to use a stored procedure in nhibernate

Hello
I try to use stored procedure in nhibernate and I found some methods :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="CO_Visites_Treeview_Sel">
exec CO_Visites_Treeview_Sel :Idclient, :Idmagasin, :Autre, :Tous
</sql-query>
</hibernate-mapping>
If I want to use the data I will use :
var query = session.GetNamedQuery("CO_Visites_Treeview_Sel");
query.SetString("Idclient", lstClients.SelectedValue.ToString());
query.SetInt32("Idmagasin", 36);
query.SetBoolean("Autre", false);
query.SetBoolean("Tous", true);
var results = query.List();
In that case I won't have intellisence neither result['colName']
I use another method :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernate.DataAccess.Models.Visites_Treeview,NHibernate.DataAccess" lazy="true">
<id name="Idvisite" column="IDVISITE" type="string">
<generator class="assigned" />
</id>
<property column="NOMMAGASIN" name="Nommagasin" type="string" />
<property column="DATEVIS" name="Datevis" type="DateTime" />
<property column="INTERVENTION" name="Intervention" type="Boolean" />
<property column="IDFACTURE" name="Idfacture" type="string" />
<property column="STATUT" name="Statut" type="byte" />
<property column="NOFACTURE" name="Nofacture" type="int" />
<property column="NODEVIS" name="Nodevis" type="int" />
<property column="LIVRE" name="Livre" type="Boolean" />
<property column="NOCOMMANDE" name="Nocommande" type="int" />
<property column="COMPTABILISEE" name="Comptabilisee" type="Boolean" />
<property column="RECUP" name="Recup" type="Boolean" />
<property column="MASQUE" name="Masque" type="Boolean" />
</class>
<sql-query name="CO_Visites_Treeview_Sel">
<return alias="Visites_Treeview" class="NHibernate.DataAccess.Models.Visites_Treeview,NHibernate.DataAccess">
<return-property column="NOMMAGASIN" name="Nommagasin" />
<return-property column="IDVISITE" name="Idvisite" />
<return-property column="DATEVIS" name="Datevis" />
<return-property column="INTERVENTION" name="Intervention" />
<return-property column="IDFACTURE" name="Idfacture" />
<return-property column="STATUT" name="Statut" />
<return-property column="NOFACTURE" name="Nofacture" />
<return-property column="NODEVIS" name="Nodevis" />
<return-property column="LIVRE" name="Livre" />
<return-property column="NOCOMMANDE" name="Nocommande" />
<return-property column="COMPTABILISEE" name="Comptabilisee" />
<return-property column="RECUP" name="Recup" />
<return-property column="MASQUE" name="Masque" />
</return>
exec CO_Visites_Treeview_Sel :Idclient, :Idmagasin, :Autre, :Tous
</sql-query>
</hibernate-mapping>
I created a bean class.
In that case results will have strongly typed object but I have some coherency problem.
But I have no real identifier so if there is two Idvisite, the second will be ignored.
I tryed to create a composite key but it doesn't work.
<composite-id>
<key-property name="Idvisite" column="IDVISITE" type="string" />
<key-property name="Idfacture" column="IDFACTURE" type="string" />
</composite-id>
And I've an error :
could not execute query
[ exec CO_Visites_Treeview_Sel #p0, #p1, #p2, #p3 ]
Name:Idclient - Value:036000004130 Name:Idmagasin - Value:36 Name:Autre - Value:False Name:Tous - Value:True
[SQL: exec CO_Visites_Treeview_Sel #p0, #p1, #p2, #p3]
Can someone tell me how to do ?
Regards
Try this, your mapping:-
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="CO_Visites_Treeview_Sel">
exec CO_Visites_Treeview_Sel :Idclient, :Idmagasin, :Autre, :Tous
</sql-query>
</hibernate-mapping>
use SetResultTransformer...
var results = Session
.GetNamedQuery("CO_Visites_Treeview_Sel")
.SetString("Idclient", lstClients.SelectedValue.ToString())
.SetInt32("Idmagasin", 36)
.SetBoolean("Autre", false)
.SetBoolean("Tous", true)
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(YOURCLASS)));
return results.List<YOURCLASS>();
and YOURCLASS is:-
public class YOURCLASS
{
public virtual int ColA { get; set; }
public virtual string COLB { get; set; }
public virtual int COLC { get; set; }
// etc..
}
HTH

How to map this in Fluent.NHibernate

I'd like to get this output from fluent.nhibernate
<map name="Dict" table="TABLE">
<key column="ID_USER" />
<index-many-to-many column="ID_TABLE" class="TableClass" />
<element column="COL" type="Int32" />
</map>
where class has:
public class User
{
public virtual IDictionary<TableClass, int> Dict { get; protected set; }
}
Closest I've got to is this:
HasMany(x => x.Dict)
.Table("TABLE")
.KeyColumn("ID_USER")
.AsMap<TableClass>("ID_TABLE")
.Element("COL");
And the output for that is:
<map name="Dict" table="TABLE">
<key>
<column name="ID_USER" />
</key>
<index type="TableClass">
<column name="ID_TABLE" />
</index>
<element type="Int32">
<column name="COL" />
</element>
<one-to-many class="Int32" /> <!-- BUG -->
</map>
How can I remove the last line (marked with BUG)?
It's not always needed (like in my example it isn't)!