一对一的 NHibernate 映射,其中所有字段都映射到单个类中的属性
我遇到的情况是,数据库在两个表之间具有一对一的关系,这两个表一起清楚地表示相同的逻辑实体。
我想将这些表映射到同一个类,并将类的属性映射到两个表中的字段。 (IE 而不是让一个类组成另一个类)。
因此,如果表格如下:
Table Foo 列:ID (PK)、描述、FavouriteCrispFlavour
表格栏 列:Id (PK)、NumberOfArms、EyeColour
我希望我的类如下所示:
public class FooBar
{
public virtual int Id {get;set;)
public virtual string Description {get;set;)
public virtual CrispFlavour FavouriteCrispFlavour {get;set;)
public virtual int NumberOfArms {get;set;)
public virtual EyeColour EyeColour {get;set;)
}
这种映射在 NHibernate 中可能吗?
I have a situation where the database has a one-to-one relationship between two tables which together clearly represent the same logical entity.
I would like to map these tables to the same class, and have properties of the class map to fields from BOTH tables. (I.E. rather than have one class compose another).
So if the tables are as follows:
Table Foo
Columns: Id (PK), Description, FavouriteCrispFlavour
Table Bar
Columns: Id (PK), NumberOfArms, EyeColour
I would like my class to look like the following:
public class FooBar
{
public virtual int Id {get;set;)
public virtual string Description {get;set;)
public virtual CrispFlavour FavouriteCrispFlavour {get;set;)
public virtual int NumberOfArms {get;set;)
public virtual EyeColour EyeColour {get;set;)
}
Is this mapping possible in NHibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这称为连接映射。您可以使用带有
table
属性的
元素来从多个表组成一个实体,例如:有关完整(和更好)的描述,请参阅 NHibernate 文档 连接映射。
Yes, it's called a join mapping. You use the
<join>
element with atable
attribute to compose an entity from multiple tables, for example:For a full (and better) description, see the NHibernate documentation on join mappings.
是的,我相信您只需要在 HQL 中指定联接即可。
Yes, I believe you just need to specify the join in the HQL.