NHibernate的一对一对2多到一 [英] NHibernate one-to-one vs 2 many-to-one

查看:248
本文介绍了NHibernate的一对一对2多到一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在他的博客,Ayende表明,使用一对一的可能是不最好的方式来实现传统的1:1的对象关系(例如customer.Name == name.Customer)

In his blog, Ayende suggests that using a one-to-one is probably not the best way to implement a traditional 1:1 object relationship (e.g. customer.Name == name.Customer).


  1. 我该如何选择当使用一个一对一关系?

  2. 为什么要选择2一到多的关系

  3. 如何进行一对一 - 酮工作(有没有生成FK柱)

一对一:

2多到一:

推荐答案

唯一的原因,我遇到了使用一个一对多映射是因业绩。

Only reason I've come across for using one-to-many mapping is because of performance.

我最初去一个一对一,直到项目击中性能问题在墙上。问题发生,因为你通常不能有延迟加载在反面一到一对一的映射。例如。当你有哪些可以(但不必)实体A对这个映射相关实体B。在这种情况下,每个实体A加载,实体B也加载。这样做是为了防止误用,如果相关的对象存在检查。代理的延迟加载会误导您认为相关的实体存在,即使它没有。如果您检查相关实体存在这样你会在一个问题

I've initially went with one-to-one until project hit the wall with performance issue. Problem happens because you usually can't have lazy loading for one-to-one mapping on reverse side. E.g. when you have entity A which can (but doesn't have to) have related entity B on that mapping. In that case, for each entity A you load, entity B is also loaded. This is done to prevent error with checking if related object exists. Proxy for lazy loading would mislead you to think that related entity exists, even when it does not. If you check for related entity existence like this you will be in a problem

if (entityA.EntityB == null) HandleNoEntityB();



但是如果用一个一对多的映射,延迟加载是没有问题的,因为开发商是工作。与它我们可以创建代理集合

If you use one-to-many mapping however, lazy loading is no problem, because developer is working with a collection for which we can create proxy.

if (entityA.EntitiesB.Count == 0) HandleNoEntityB();

这并不一定是一个问题,如果你可以让你的系统的假设,即实体A总是有且只有一个相关实体B.在这种情况下,您应该设置在该映射contrained =真。

This doesn't have to be a problem if you can make an assumption in your system that entity A always has exactly one related entity B. In that case, you should set contrained="true" on that mapping.

这篇关于NHibernate的一对一对2多到一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆