是一对的一个或组件? NHibernate的映射 [英] Is it one-to-one or Component ? Nhibernate Mapping

查看:84
本文介绍了是一对的一个或组件? NHibernate的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有Person和Address类和它们在数据库不同的表。

Assume that I have Person and Address Classes and they have different tables in DB.

一个人能有一个地址类的吧。这对我来说是重要的组成部分。一个人能只有一个地址,因为它的孩子元素,我想,让他们在不同的表在数据库中。

One Person can have one Address Class in it. This is the important part for me. One Person can have only one Address as it s child element and i want to keep them in different tables in DB.

通常情况下,我知道我需要保持他们在同一个表,但是我的情况下需要这个。

Normally i know that i need to hold them in the same table, but my scenario needs this.

(也它看起来像一个一对多的,但我不希望有一个集合只为一个对象)

Person     Address
-------    -------
id         id
Name       PersonId
           StreetName

和类code

Person                                                 
--------
public virtual long Id{get;set;}
public virtual Address MyAddress {get;set;}

正如你看,我想地址属性的人,当我得到任何的人吗?但他们在不同的表。怎么可以这样映射做?

As you see i want to get Address Property in Person when i get any Person ? But they are in different tables. How can this mapping be done ?

在此先感谢

推荐答案

使用分量,因为它有它自己的表不能映射地址。你可以使用它,如果你只在一个表中有各个领域。 从Hibernate参考:

You can't map Address using a component because it has its own table. You could use it if you had all fields in just one table. From the Hibernate reference:

该组件元素把子对象的属性,父类的表中的列。

The component element maps properties of a child object to columns of the table of a parent class.

您应该使用一个一对一的关系映射表和类在你的情况。

You should use a one-to-one relationship to map the tables and classes in you scenario.

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

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