You would map a second entity (that has the correct column names, and table
name for the auditing information) and then access the session (or create a
child session) from inside the listener and then call save.

Here is a decent example:
http://darrell.mozingo.net/2009/08/31/auditing-with-nhibernate-listeners/

<http://darrell.mozingo.net/2009/08/31/auditing-with-nhibernate-listeners/>Hope
that helps!

- Robert

On Tue, Apr 13, 2010 at 2:33 AM, Wouter <[email protected]> wrote:

> Thank you for your replies.
>
> @Robert: I understand and agree that EventListerens are a good place
> to put this logic for this audit table. So the "where" is clear, but
> the "how" is my question. How do I get NHibernate to write the data of
> a class to a second table? So I have mapped my entity to the [Entity]
> table, but how do I write data also to the [Entity]_History table?
>
> @Fabio: Ok, good to know that this is possible, but how do I let
> NHibernate know to what table it should write the data? Basically, in
> one table an INSERT or UPDATE is done, in the second (History) table
> it is always an INSERT.
>
> Grtz, Wouter
>
>
> On Apr 12, 7:32 pm, Fabio Maulo <[email protected]> wrote:
> > You can map the same entity with two different tables and even an whole
> > graph.
> > <class name="Customer" table="Customers">
> > <class name="Customer" table="CustomersAudit"
> entity-name="CustomerAudit">
> >
> > 2010/4/12 Robert Rudduck <[email protected]>
> >
> >
> >
> >
> >
> > > What Oskar mentioned is the correct way to handle this. You use
> > > EventListeners to catch the save or update of the Person entity and
> write
> > > out the corresponding history entries. You cant map the same class to
> > > multiple tables in such a way that it generates inserts.
> >
> > > This is an auditing problem. EventHandlers are really the best way to
> > > handle that type of issue.
> >
> > > - Robert
> >
> > > On Mon, Apr 12, 2010 at 10:08 AM, Wouter <[email protected]>
> wrote:
> >
> > >> Hi Oskar,
> >
> > >> Thank you for your reply. Usefull links indeed, but my main problem is
> > >> how to store the same entity in two different tables in my database. A
> > >> small example:
> >
> > >> I have an entity Person with properties Id and Name, mapped to a table
> > >> Person with columns PersonId and Name. So far so easy, but now comes
> > >> another requirement that all changes to the Person table should also
> > >> be stored in a Person_History table, with columns Person_HistoryId and
> > >> a Date besides PersonId and Name of course. In realty there are many
> > >> more properties and corresponding columns, but the point is that these
> > >> columns are present in both the Person and the Person_History tables,
> > >> and both these tables need to be filled.
> >
> > >> We would like to use the same entity to fill both tables, but we are
> > >> not sure if that is possible, and if so, how to do the mapping for
> > >> that. Any suggestions...?
> >
> > >> Cheers
> >
> > >> On Apr 12, 4:52 pm, Oskar Berggren <[email protected]> wrote:
> > >> > Have you looked at NHibernate event listeners and interceptors?
> >
> > >> >http://nhforge.org/doc/nh/en/index.html#events
> >
> > >> > Some example:
> > >>http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventl.
> ..
> >
> > >> > /Oskar
> >
> > >> > 2010/4/12 Wouter <[email protected]>:
> >
> > >> > > Hi,
> >
> > >> > > We are using (Fluent) NHibernate to store our entities in a
> database.
> > >> > > For every change to the database we have to add an entry to a
> > >> > > [Entity]_History table, where [Entity] is the name of our Entity
> (also
> > >> > > the name of or database table for that entity). We thought about
> using
> > >> > > a trigger in the database, but due to business requirements this
> is
> > >> > > not an option.
> >
> > >> > > A possibe solution would be to put the logic in our code so we do
> two
> > >> > > inserts/updates in our database, the first to the [Entity]_History
> > >> > > table and the second to the [Entity] table. For this we could add
> an
> > >> > > extra property to the entity in which we indicate what table
> should be
> > >> > > inserted/updated. Would a solution like this be possible? If so,
> how
> > >> > > do we create two mappings for the same entity but for two
> different
> > >> > > tables based on the value of that extra property?
> >
> > >> > > Or is there another (generic) way to implement this with
> NHibernate?
> >
> > >> > > Thanks!
> >
> > >> > > --
> > >> > > You received this message because you are subscribed to the Google
> > >> Groups "nhusers" group.
> > >> > > To post to this group, send email to [email protected].
> > >> > > To unsubscribe from this group, send email to
> > >> [email protected]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>
> ­>
> > >> .
> > >> > > For more options, visit this group athttp://
> > >> groups.google.com/group/nhusers?hl=en.- Hide quoted text -
> >
> > >> > - Show quoted text -
> >
> > >> --
> > >> You received this message because you are subscribed to the Google
> Groups
> > >> "nhusers" group.
> > >> To post to this group, send email to [email protected].
> > >> To unsubscribe from this group, send email to
> > >> [email protected]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>
> ­>
> > >> .
> > >> For more options, visit this group at
> > >>http://groups.google.com/group/nhusers?hl=en.
> >
> > >  --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "nhusers" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>
> ­>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/nhusers?hl=en.
> >
> > --
> > Fabio Maulo- Hide quoted text -
> >
> > - Show quoted text -
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
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.

Reply via email to