views:

464

answers:

3

I have this table mapping (details don't really matter I think):

WithTable("COPACKER_FACILITY");  
Id(x => x.FacilityNumber, "FACILITY_NUM").GeneratedBy.Sequence("FACSEQ");
Map(x => x.FacilityName, "FACILITY_NAME").Not.Nullable().Trimmed();
Map(x => x.AddressLine1, "ADDR1").Not.Nullable().Trimmed();
...
WithTable("FACIL_OTH_AUDIT_INFO", m =>
   {
      m.WithKeyColumn("FACILITY_NUM");
      m.Map(x => x.ProdnShiftsNum, "PRODN_SHIFTS_NUM").Not.Nullable();
      m.Map(x => x.ProdnCapacity, "PRODN_CAPACITY").Not.Nullable();
      m.Map(x => x.ProdnLinesNum, "PRODN_LINES_NUM").Not.Nullable();
      m.Map(x => x.AuditScore, "AUDIT_SCORE");
      m.References(x => x.FacilStatus, "STATUS_IND").Not.Nullable();
   });
HasMany(x => x.ComplianceFlags)
   .KeyColumnNames.Add("FACILITY_NUM")
   .Inverse()
   .Cascade.All();
...

The reason for the one to one table is for audit reasons. There's a FACIL_OTH_AUDIT_INFO_HIST table that should get a record for every insert and update in the main table.

My question: How can I know when an insert or update happens in that table so I know to insert an audit record?

Many thanks!

A: 

You can use event listeners.

try http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx

kvalcanti
I appreciate the link, but if you read the "Solution" section, it doesn't appear to be even close to my scenario.
Rob
+2  A: 

+1 to what kvalcanti said... here's another post that I think explains it a little better though (and shows you how to do it without XML configuration!). I'm doing what this guy is doing on my project and it's working really well.

http://www.codinginstinct.com/2008/04/nhibernate-20-events-and-listeners.html

Caveat: I'm not inserting new objects that need to be saved in this event in my project, which I assume will not be a problem, but I can't say for sure since I'm not doing exactly what you're doing.

Jon Kruger
+2  A: 

I posted the final solution to this problem and thought I'd share

http://robtennyson.us/post/2009/08/23/NHibernate-Interceptors.aspx

Rob
Toran Billups