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