是一对的一个或组件? NHibernate的映射组件、是一对、NHibernate

由网友(独守空城待佳人)分享简介:假设我有Person和Address类和它们在数据库不同的表。Assume that I have Person and Address Classes and they have different tables in DB.一个人能有一个地址类的吧。这对我来说是重要的组成部分。一个人能只有一个地址,因为它的孩子...

假设我有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.

阅读全文

相关推荐

最新文章