开发者

Hibernate implementation of simple many-to-many relationships - Dictionary or List?

I have 3 DB Tables: Person, Address, and PersonAddress. Person Address is a simple join table (only stores the IDs of Person and Address).

In my domain model, I have Person and Address. Person is configured to have a many-to-many relationship to Address (through PersonAddress). In code, this is implemented with List<Address>.

I've been told that I will get better performance from nHibernate if I...

  1. Create a PersonAddress domain object
  2. Configure Person to have a one-to-many relationship to PersonAddress
  3. Configure a a many-to-many index for Address on this relationship
  4. Implement it in code with a Dictiona开发者_StackOverflowry<Address, PersonAddress>

Is this true?


I wouldn't create an unnecessary class in the domain just because of that. The use of a dictionary for better performance also sounds weird to me.

You could use an idbag, this is faster to update. (This is mapped as a List in the domain, but has an id in the database).

There are always different things to consider about performance. There is a very interesting chapter in the reference documentation about collection performance.


I also would not introduce a complex mapping for the sakes of performance in NHibernate until the need for doing so is established.

Along with the standard many-to-many mapping you can use the a one-to-many strategy with the PersonAddress object (PersonAddress has a collection of Persons and a collection of Addresses). This strategy is used when you want to maintain additional information about the Person-Address relationship.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜