[英]Class mapping with NHibernate
I have a plain database table (Named DBFoo): 我有一个普通的数据库表(名为DBFoo):
| PropertyA | PropertyB| PropertyC | PropertyD | PropertyE |
PropertyA, PropertyB and PropertyC are part of the key. PropertyA,PropertyB和PropertyC是键的一部分。
And for this in my program I have following class structure: 为此,在我的程序中,我具有以下类结构:
public class Foo
{
public virtual SubFoo SubFoo { get; set; }
public virtual string PropertyC { get; set; }
public virtual string PropertyD { get; set; }
public virtual string PropertyE { get; set; }
}
public class SubFoo
{
public virtual string PropertyA { get; set; }
public virtual string PropertyB { get; set; }
}
Now I'm trying to create the mapping file: 现在,我正在尝试创建映射文件:
...
<class name="Foo" table="DBFoo">
<composite-id>
// here I Need to define the mapping for the SubFoo properties PropertyA and PropertyB
<key-property name="PropertyC" column="PropertyC"/>
</composite-id>
<property name="PropertyD" column="PropertyD"/>
<property name="PropertyE" column="PropertyE"/>
</class>
....
Anyone an idea how I can define the key-properties for the PropertyA and PropertyB? 有人知道如何为PropertyA和PropertyB定义键属性吗?
Thanks in advance for helping 预先感谢您的帮助
Possibly is a bad idea, but you can try to generate a FooIdentifier
class that decorates your SubFoo
class to provide direct access to PropertyA
, PropertyB
and PropertyC
. 可能不是一个好主意,但是您可以尝试生成一个FooIdentifier
类来装饰SubFoo
类,以提供对PropertyA
, PropertyB
和PropertyC
直接访问。
public class FooIdentifier
{
private SubFoo InnerSubFoo { get; set; }
public FooIdentifier(SubFoo subFoo, string propertyC)
{
this.InnerSubFoo = subFoo
this.PropertyC = propertyC;
}
public virtual string PropertyA
{
get
{
return SubFoo.PropertyA;
}
set
{
SubFoo.PropertyA = value;
}
}
public virtual string PropertyB
{
get
{
return SubFoo.PropertyB;
}
set
{
SubFoo.PropertyB = value;
}
}
public virtual string PropertyC { get; set; }
}
Then, the mapping file could be something like: 然后,映射文件可能类似于:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Foo" table="Foo" lazy="true" >
<composite-id name="FooIdentifier" class="FooIdentifier">
<key-property name="PropertyA" column="PropertyA" />
<key-property name="PropertyB" column="PropertyB" />
<key-property name="PropertyC" column="PropertyB" />
</composite-id>
<property name="PropertyD" column="PropertyD" type="String" />
<property name="PropertyE" column="PropertyE" type="String" />
</class>
</hibernate-mapping>
As HuorSwords mentioned, you can define a composite key for your table, to do this, you have to create a composite key class which contains the properties AND overwrites the hashkey and equals methods... 正如HuorSwords提到的那样,您可以为表定义一个复合键,为此,您必须创建一个复合键类,其中包含属性并覆盖hashkey和equals方法...
Read this blog for further details of how exactly this should be implemented. 请阅读此博客,以获取有关应如何实施的更多详细信息。
But I would strongly recommend not to use composite keys because there can be some issues in general. 但是我强烈建议您不要使用组合键,因为通常会出现一些问题。 In addition it is recommended to have a surrogate key more or less in each table and have secondary keys/foreign keys where needed. 另外,建议在每个表中或多或少具有代理键 ,并在需要时具有辅助键/外键。
This makes also the nhibernate mappings a lot easier! 这也使nhibernate映射更加容易!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.