Hi guys,
I am trying to figure out how to map the following situation using
NHibernate's "sexy" mapping by code system.
Please help as I have been trying to figure this out for a while with no
luck! I am using components to represent the composite keys.
*Account*
BSB (PK)
AccountNumber (PK)
Name
*Transactions*
BSB (PK, FK)
AccountNumber (PK, FK)
Amount
public class Account
{
public virtual AccountKey Key { get; set; }
public virtual float Amount { get; set; }
public ICollection<Transaction> Transactions { get; set; }
}
public class AccountKey
{
public virtual int BSB { get; set; }
public virtual int AccountNumber { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = obj as AccountKey;
if (t == null)
return false;
if (BSB == t.BSB && AccountNumber == t.AccountNumber)
return true;
return false;
}
public override int GetHashCode()
{
return (BSB.ToString() + "|" +
AccountNumber.ToString()).GetHashCode();
}
}
public class AccountMapping : ClassMapping<Account>
{
public AccountMapping()
{
Table("Accounts");
ComponentAsId(x => x.Key, map =>
{
map.Property(p => p.BSB);
map.Property(p => p.AccountNumber);
});
Property(x => x.Amount);
Bag(x => x.Transactions, collectionMapping =>
{
//How do I map the composite foreign key here?
//collectionMapping.Key(???);
collectionMapping.Inverse(true);
collectionMapping.Cascade(Cascade.DeleteOrphans);
},
r => r.OneToMany());
}
}
public class Transaction
{
//How do I represent a composite primary key that is also a foreign key?
public virtual TransactionKey Key { get; set; }
public virtual string Name { get; set; }
public virtual Account Account { get; set; }
}
public class TransactionKey
{
public virtual int BSB { get; set; }
public virtual int AccountNumber { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = obj as TransactionKey;
if (t == null)
return false;
if (BSB == t.BSB && AccountNumber == t.AccountNumber)
return true;
return false;
}
public override int GetHashCode()
{
return (BSB.ToString() + "|" +
AccountNumber.ToString()).GetHashCode();
}
}
public class TransactionMapping : ClassMapping<Transaction>
{
public TransactionMapping()
{
Table("Transactions");
ComponentAsId(x => x.Key, map =>
{
map.Property(p => p.BSB);
map.Property(p => p.AccountNumber);
});
Property(x => x.Name);
ManyToOne(x => x.Account, x =>
{
//How do I map the composite keys here?
//x.Column(???);
});
}
}
Please tell me this is possible!
Prabu
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/nhusers/-/2vHT7Ax57CoJ.
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.