Tutorial :NHibernate Table Update Event



Question:

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!


Solution:1

+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.


Solution:2

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

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


Solution:3

You can use event listeners.

try http://nhibernate.info/doc/howto/various/creating-an-audit-log-using-nhibernate-events.html


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »