简体   繁体   English

NHibernate的多对多效率低下?

[英]NHibernate many-to-many inefficiency?

I'm trying to setup a sample project using NHibernate and Fluent NHibernate. 我正在尝试使用NHibernate和Fluent NHibernate设置一个示例项目。 I am using the example mappings from the Fluent NHibernate web-site. 我正在使用Fluent NHibernate网站上的示例映射

My question is about the many-to-many mapping between Store and Product. 我的问题是关于商店和产品之间的多对多映射。 It seems (when looking at the generated SQL) that when adding a product to the store, NHibernate deletes all the records from the association table (StoreProduct) that belong to that store, and then inserts all the records again, now including the association to the new product I added. 看起来(在查看生成的SQL时),当将产品添加到商店时,NHibernate会删除属于该商店的关联表(StoreProduct)中的所有记录,然后再次插入所有记录,现在包括关联到我添加的新产品。

Is this the default behavior or am I missing something? 这是默认行为还是我遗漏了什么? It just seems not very efficient to delete and re-insert all the associations every time I need to add one. 每次我需要添加一个关联时,删除和重新插入所有关联似乎不是很有效。

This is expected behavior. 这是预期的行为。 I believe this should only happen when you use the bag mapping strategy which it looks like they are using in the example. 我相信这应该只在您使用它在示例中使用的映射策略时才会发生。 A bag indicates that there is an unordered collection that can have duplicate items. 一个包表示存在可以具有重复项的无序集合。 Because bag items are not unique NHibernate cannot tell when you've added or removed an item from a bag easily. 因为袋子物品不是唯一的,所以NHibernate无法分辨您何时从袋子中轻松添加或移除物品。 The easiest thing for NHibernate then is to do is delete all associations and then re-add. NHibernate最简单的方法是删除所有关联,然后重新添加。

It's been a while since I've played with many-to-many mappings (I usually just map as two one-to-many relationships) but I believe that if you use use a different construct, for example, a set (which does not allow duplicates) you should find that the behavior is different. 我玩了多对多的映射已经有一段时间了(我通常只是映射为两个一对多的关系)但是我相信如果你使用一个不同的构造,例如,一个集合(不允许重复)你应该发现行为是不同的。 Of course, you should use what ever construct makes the most semantic sense for your application. 当然,您应该使用构造对您的应用程序最具语义意义的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM