Fluent NHibernate; collection management
I have a very simple situation but I can't seem to get my head around; I ha开发者_开发知识库ve a table promotions which has many sites. The site can be used with different promotions and in my (postgres) database I've 3 tables; promotions, sites and promotions_sites. In my web application a user can edit the promotion and add a collection of sites (new line seperated). So in a save the collection of sites is saved at the promotion. This works. Still there are 2 problems; 1) old site records are not removed (when one is delete from the lines of sites) 2) when a current site is saved all original sites are re-created
My question is at which level I should manage the sites; 1) application level; just deleting all sites before re-inserting 2) data level; is there a nhibernate configuration to do this? 3) database level; create triggers/cascade deletes on the site table based upon the absence of an item in promotions_sites
At first the automatical orphan deletes. Sites can't be deleted automatically when your relation is defined as many-to-many
. But if your promotions_sites
table is mapped as separate entity and you have two one-to-many
relations in sites and promotions, you can achieve automatic deletes by setting cascade on those relations to all-delete-orphan
.
Second thing - where to manage the collection. You should let NHibernate to do this. Assuming you have proper mappings, you should not care about it on application level or especially at database trigger level. Anyway, the logic of handling collections in NHibernate is somehow different that you may be familiar with from database level. Generally you need to load parent object with the existing child collection, modify the collection and commit back the changes to the database. You should not replace the whole collection.
But it will be easier to talk when you show your mappings and code used to save changes.
精彩评论