views:

25

answers:

2

Hi, looking at an existing NHibernate implementation that maps a single class to two database tables that are joined. The current functionality is read-only. The table join is actually hidden away via a readonly view and it's the view that's referred to in the NHibernate mapping. Works fine for readonly behaviour. Except I need to add Insert, Update, Delete behaviour and only one of the tables needs to be inserted/updated/deleted. How best to do this? I can take readonly off the view of course, I could replicate the join in the NHibernate mapping, but how do I tell NHibernate that insert/update/delete affects one table only?

Thanks DC

A: 

You can map to a view instead of a table, then define custom sql for your inserts, updates and deletes. I would recommend calling a stored procedure.

See the docs: http://nhforge.org/doc/nh/en/index.html#querysql-cud

I've used this technique quite a lot and it works fine.

cbp
A: 

You can set the update and insert attributes on the property mappings to false and they will be excluded from updates and inserts:

<property name="MyProperty" update="false" insert="false" />

Keep in mind that your view will need to have a single primary key defined to enable updates or inserts.

Jamie Ide