所以我使用流利的nhibernate。
我有3个表和3个映射文件:具有PersonId和BankId的PersonAccount,以及对PersonAccount一无所知的Bank和Person表。
在我的表单中,我选择人员和银行名称,然后搜索这些表格以填充我的personaccount,如下面的代码所示。
[Activity]
public PersonAccount Save(PersonAccount PersonAccount)
{
BankService bs = new BankService();
Bank bank = bs.GetBankForName(PersonAccount.BankName);
PersonService ps = new PersonService();
Person person = ps.GetForName(PersonAccount.PersonName);
PersonAccount.Bank = bank;
bank.PersonAccounts.Add(PersonAccount);
PersonAccount.Person = person;
person.PersonAccounts.Add(PersonAccount);
base.Save(PersonAccount);
return PersonAccount;
}
PersonMap:
HasMany(x => x.PersonAccounts).KeyColumn("PERSONID").Cascade.None().Not.KeyUpdate();
BankMap
HasMany(x => x.PersonAccounts).KeyColumn("BANKID").Cascade.None().Not.KeyUpdate();
PersonAccountmap
References(x => x.Bank, "BANKID").Not.Update();
References(x => x.Person, "PERSONID").Not.Update();
这是我的日志输出:
[2014-10-24 13:16:41,999|23 |DEBUG|NHibernate.SQL ] select BankId1_,Active1_,BankCode1_,BankName1_,ContactE5_1_,ContactN6_1_,ContactP7_1_,FromDate1_,ToDate1_,IsDeleted1_,Creatio11_1_,Modific12_1_,CreatorId1_,Modific14_1_ from ( select bank0_.BankId as BankId1_, bank0_.Active as Active1_, bank0_.BankCode as BankCode1_, bank0_.BankName as BankName1_, bank0_.ContactEmail as ContactE5_1_, bank0_.ContactName as ContactN6_1_, bank0_.ContactPhone as ContactP7_1_, bank0_.FromDate as FromDate1_, bank0_.ToDate as ToDate1_, bank0_.IsDeleted as IsDeleted1_, bank0_.CreationDate as Creatio11_1_, bank0_.ModificationDate as Modific12_1_, bank0_.CreatorId as CreatorId1_, bank0_.ModificatorId as Modific14_1_ from Bank bank0_ where bank0_.BankName=:p0 ) where rownum <=1;:p0 = 'Raiffaisen' [Type: AnsiString (0)] |
[2014-10-24 13:16:42,346|23 |DEBUG|NHibernate.SQL ] select PersonID8_,DateOfBi2_8_,Educatio3_8_,Employme4_8_,FatherName8_,FirstName8_,GenderID8_,Identifi8_8_,LastName8_,Marital10_8_,Nationa11_8_,PersonL12_8_,PersonT13_8_,Profess14_8_,ReligionID8_,Residen16_8_,Country17_8_,Municip18_8_,CantonId8_,City8_,LocationId8_,PersonCode8_,DELETED8_,Creatio24_8_,Modific25_8_,CreatorId8_,Modific27_8_ from ( select person0_.PersonID as PersonID8_, person0_.DateOfBirth as DateOfBi2_8_, person0_.EducationLevelID as Educatio3_8_, person0_.EmploymentStatusID as Employme4_8_, person0_.FatherName as FatherName8_, person0_.FirstName as FirstName8_, person0_.GenderID as GenderID8_, person0_.IdentificationNumberId as Identifi8_8_, person0_.LastName as LastName8_, person0_.MaritalStatusID as Marital10_8_, person0_.NationalityID as Nationa11_8_, person0_.PersonLifeStatusID as PersonL12_8_, person0_.PersonTypeID as PersonT13_8_, person0_.ProfessionID as Profess14_8_, person0_.ReligionID as ReligionID8_, person0_.ResidentalStatusID as Residen16_8_, person0_.CountryOfBirth as Country17_8_, person0_.MuniciplityId as Municip18_8_, person0_.CantonId as CantonId8_, person0_.City as City8_, person0_.LocationId as LocationId8_, person0_.PersonCode as PersonCode8_, person0_.DELETED as DELETED8_, person0_.CreationDate as Creatio24_8_, person0_.ModificationDate as Modific25_8_, person0_.CreatorId as CreatorId8_, person0_.ModificatorId as Modific27_8_ from Person person0_ where ((person0_.FirstName||:p0)||person0_.LastName)=:p1 ) where rownum <=1;:p0 = ' ' [Type: String (0)], :p1 = 'ALEN KOPIC' [Type: String (0)] |
[2014-10-24 13:16:42,746|23 |DEBUG|NHibernate.SQL ] SELECT personacco0_.BANKID as BANKID2_, personacco0_.AccountId as AccountId2_, personacco0_.AccountId as AccountId7_1_, personacco0_.Active as Active7_1_, personacco0_.FromDate as FromDate7_1_, personacco0_.ToDate as ToDate7_1_, personacco0_.Note as Note7_1_, personacco0_.ReferenceNumber as Referenc6_7_1_, personacco0_.AccountNumber as AccountN7_7_1_, personacco0_.CreationDate as Creation8_7_1_, personacco0_.ModificationDate as Modifica9_7_1_, personacco0_.CreatorId as CreatorId7_1_, personacco0_.ModificatorId as Modific11_7_1_, personacco0_.BANKID as BANKID7_1_, personacco0_.PERSONID as PERSONID7_1_, person1_.PersonID as PersonID8_0_, person1_.DateOfBirth as DateOfBi2_8_0_, person1_.EducationLevelID as Educatio3_8_0_, person1_.EmploymentStatusID as Employme4_8_0_, person1_.FatherName as FatherName8_0_, person1_.FirstName as FirstName8_0_, person1_.GenderID as GenderID8_0_, person1_.IdentificationNumberId as Identifi8_8_0_, person1_.LastName as LastName8_0_, person1_.MaritalStatusID as Marital10_8_0_, person1_.NationalityID as Nationa11_8_0_, person1_.PersonLifeStatusID as PersonL12_8_0_, person1_.PersonTypeID as PersonT13_8_0_, person1_.ProfessionID as Profess14_8_0_, person1_.ReligionID as ReligionID8_0_, person1_.ResidentalStatusID as Residen16_8_0_, person1_.CountryOfBirth as Country17_8_0_, person1_.MuniciplityId as Municip18_8_0_, person1_.CantonId as CantonId8_0_, person1_.City as City8_0_, person1_.LocationId as LocationId8_0_, person1_.PersonCode as PersonCode8_0_, person1_.DELETED as DELETED8_0_, person1_.CreationDate as Creatio24_8_0_, person1_.ModificationDate as Modific25_8_0_, person1_.CreatorId as CreatorId8_0_, person1_.ModificatorId as Modific27_8_0_ FROM PersonAccounts personacco0_ left outer join Person person1_ on personacco0_.PERSONID=person1_.PersonID WHERE personacco0_.BANKID=:p0;:p0 = 17 [Type: Int32 (0)] |
[2014-10-24 13:16:43,652|23 |DEBUG|NHibernate.SQL ] SELECT personacco0_.PERSONID as PERSONID2_, personacco0_.AccountId as AccountId2_, personacco0_.AccountId as AccountId7_1_, personacco0_.Active as Active7_1_, personacco0_.FromDate as FromDate7_1_, personacco0_.ToDate as ToDate7_1_, personacco0_.Note as Note7_1_, personacco0_.ReferenceNumber as Referenc6_7_1_, personacco0_.AccountNumber as AccountN7_7_1_, personacco0_.CreationDate as Creation8_7_1_, personacco0_.ModificationDate as Modifica9_7_1_, personacco0_.CreatorId as CreatorId7_1_, personacco0_.ModificatorId as Modific11_7_1_, personacco0_.BANKID as BANKID7_1_, personacco0_.PERSONID as PERSONID7_1_, bank1_.BankId as BankId1_0_, bank1_.Active as Active1_0_, bank1_.BankCode as BankCode1_0_, bank1_.BankName as BankName1_0_, bank1_.ContactEmail as ContactE5_1_0_, bank1_.ContactName as ContactN6_1_0_, bank1_.ContactPhone as ContactP7_1_0_, bank1_.FromDate as FromDate1_0_, bank1_.ToDate as ToDate1_0_, bank1_.IsDeleted as IsDeleted1_0_, bank1_.CreationDate as Creatio11_1_0_, bank1_.ModificationDate as Modific12_1_0_, bank1_.CreatorId as CreatorId1_0_, bank1_.ModificatorId as Modific14_1_0_ FROM PersonAccounts personacco0_ left outer join Bank bank1_ on personacco0_.BANKID=bank1_.BankId WHERE personacco0_.PERSONID=:p0;:p0 = 897483 [Type: Int32 (0)] |
[2014-10-24 13:17:45,279|23 |DEBUG|NHibernate.SQL ] select PERSONACCOUNTS_SEQ.nextval from dual |
[2014-10-24 13:17:45,303|23 |DEBUG|NHibernate.SQL ] Batch commands:
command 0:INSERT INTO PersonAccounts (Active, FromDate, ToDate, Note, ReferenceNumber, AccountNumber, CreationDate, ModificationDate, CreatorId, ModificatorId, BANKID, PERSONID, AccountId) VALUES (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12);:p0 = '0' [Type: AnsiString (0)], :p1 = 10/14/2014 12:00:00 AM [Type: DateTime (0)], :p2 = 10/30/2014 12:00:00 AM [Type: DateTime (0)], :p3 = NULL [Type: AnsiString (0)], :p4 = NULL [Type: AnsiString (0)], :p5 = 'SASASA' [Type: AnsiString (0)], :p6 = 10/24/2014 1:17:45 PM [Type: DateTime (0)], :p7 = 10/24/2014 1:17:45 PM [Type: DateTime (0)], :p8 = '-1' [Type: AnsiString (0)], :p9 = NULL [Type: AnsiString (0)], :p10 = 17 [Type: Int32 (0)], :p11 = 897483 [Type: Int32 (0)], :p12 = 143 [Type: Int32 (0)]
|
[2014-10-24 13:17:45,306|23 |DEBUG|NHibernate.SQL ] Batch commands:
command 0:UPDATE Person SET DateOfBirth = :p0, EducationLevelID = :p1, EmploymentStatusID = :p2, FatherName = :p3, FirstName = :p4, GenderID = :p5, IdentificationNumberId = :p6, LastName = :p7, MaritalStatusID = :p8, NationalityID = :p9, PersonLifeStatusID = :p10, PersonTypeID = :p11, ProfessionID = :p12, ReligionID = :p13, ResidentalStatusID = :p14, CountryOfBirth = :p15, MuniciplityId = :p16, CantonId = :p17, City = :p18, LocationId = :p19, PersonCode = :p20, DELETED = :p21, CreationDate = :p22, ModificationDate = :p23, CreatorId = :p24, ModificatorId = :p25 WHERE PersonID = :p26;:p0 = 12/25/1926 1:00:00 AM [Type: DateTime (0)], :p1 = 0 [Type: Int32 (0)], :p2 = 0 [Type: Int32 (0)], :p3 = NULL [Type: AnsiString (0)], :p4 = 'ALEN' [Type: AnsiString (0)], :p5 = 0 [Type: Int32 (0)], :p6 = '2110992173049' [Type: AnsiString (0)], :p7 = 'KOPIC' [Type: AnsiString (0)], :p8 = 0 [Type: Int32 (0)], :p9 = 0 [Type: Int32 (0)], :p10 = 1 [Type: Int32 (0)], :p11 = 1 [Type: Int32 (0)], :p12 = 0 [Type: Int32 (0)], :p13 = 0 [Type: Int32 (0)], :p14 = 0 [Type: Int32 (0)], :p15 = 0 [Type: Int32 (0)], :p16 = 0 [Type: Int32 (0)], :p17 = 0 [Type: Int32 (0)], :p18 = NULL [Type: AnsiString (0)], :p19 = 1 [Type: Int32 (0)], :p20 = '00123' [Type: AnsiString (0)], :p21 = 'N' [Type: AnsiString (0)], :p22 = NULL [Type: DateTime (0)], :p23 = NULL [Type: DateTime (0)], :p24 = NULL [Type: AnsiString (0)], :p25 = NULL [Type: AnsiString (0)], :p26 = 897483 [Type: Int32 (0)]
|
我真的不明白。我已经做了一切我可以映射fies以防止这些更新。 希望你能帮帮我。
答案 0 :(得分:0)
为什么你有来自Bank / Person To PersonAccount的HasMany,如果这些classe不知道PersonAccount? 您应该只在PersonAccount映射中使用引用(x =&gt; x.Bank ...)和引用(x =&gt; x.Person ...)。