流畅的NHibernate;收藏管理
我的情况很简单,但我似乎无法理解;我有一个表促销,其中有许多网站。该网站可以与不同的促销一起使用,并且在我的(postgres)数据库中我有3个表;促销、网站和促销网站。 在我的网络应用程序中,用户可以编辑促销并添加网站集合(换行符分隔)。因此,在保存中,网站集合会在促销时保存。这有效。还有2个问题; 1) 旧站点记录不会被删除(当从站点行中删除一条记录时) 2)保存当前站点时,将重新创建所有原始站点
我的问题是我应该在哪个级别管理站点; 1)应用层面;只需在重新插入之前删除所有网站 2)数据级别;有没有一个 nhibernate 配置可以做到这一点? 3)数据库级;根据 Promotions_sites 中缺少项目,在站点表上创建触发器/级联删除
I have a very simple situation but I can't seem to get my head around; I have 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先自动孤立删除。当您的关系定义为
多对多
时,网站无法自动删除。但是,如果您的promotions_sites
表映射为单独的实体,并且您在 sites 和 promotions一对多关系em>,您可以通过将这些关系上的级联设置为all-delete-orphan
来实现自动删除。第二件事 - 在哪里管理集合。你应该让 NHibernate 来做这件事。假设您有正确的映射,您不应该在应用程序级别或特别是在数据库触发器级别上关心它。无论如何,NHibernate 中处理集合的逻辑与您在数据库级别可能熟悉的有所不同。通常,您需要使用现有子集合加载父对象,修改集合并将更改提交回数据库。您不应该替换整个集合。
但是,当您显示用于保存更改的映射和代码时,对话会更容易。
At first the automatical orphan deletes. Sites can't be deleted automatically when your relation is defined as
many-to-many
. But if yourpromotions_sites
table is mapped as separate entity and you have twoone-to-many
relations in sites and promotions, you can achieve automatic deletes by setting cascade on those relations toall-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.