在 Fluent NHibernate 中处理与值类型的一对多关系
我正在致力于将应用程序迁移到 NHibernate,并且我正在使用 Fluent NHibernate。我遇到将值类型集合映射到聚合根的问题。
我有一个 PhoneNumber
值类型,它有几个属性 - Number
、NumberType
和 Description
。我的域中有许多对象包含电话号码集合。
在数据库 (SQL 2008) 中,这些值保存在不同的表中。因此,我可能有 Customers
和 CustomerPhoneNumbers
以及 Vendors
和 VendorPhoneNumbers
。除了将它们与其父级相关联的外键之外,电话号码表是相同的。
问题是我想使用简单的 PhoneNumber
值类型,而不必创建具有与它们相关的属性的 CustomerPhoneNumber
和 VendorPhoneNumber
类型他们的父类型,我不知道如何在 NHibernate 中实现这一点。这是可能的还是我需要更改我的域对象以更紧密地匹配底层数据库架构?
更新:更多信息
看来我什至无法让基本地图用于检索。这是我所拥有的一个简化示例:
public class CustomerMap : ClassMap<Customer>
{
public CustomerMap()
{
Table("Customers");
Id(x => x.Id);
Map(x => x.Name);
Component(x => x.Address);
HasMany(x => x.PhoneNumbers)
.KeyColumn("CustomerId")
.Cascade.All()
.Table("CustomerPhoneNumbers");
}
}
public class PhoneNumberMap : ClassMap<PhoneNumber>
{
public PhoneNumberMap()
{
Id(x => x.Id);
Map(x => x.Number, "PhoneNumber");
Map(x => x.PhoneNumberType);
Map(x => x.Description);
}
}
看起来 SQL 没有正确生成。当我尝试查看客户的 PhoneNumbers 列表时,收到一个 ADO 错误,该错误表明 NHibernate 正在查找名为 PhoneNumber
的表。
看起来它没有选择 Table("CustomerPhoneNumbers")
部分,而是默认使用与对象名称相同的表。但我无法在 PhoneNumberMap 中指定表,因为它会根据我们要提取的聚合根而有所不同。
I'm working on migrating an application to NHibernate, and I'm using Fluent NHibernate. I'm running into an issue mapping a collection of value types to an aggregate root.
I have a PhoneNumber
value type that has a few properties — Number
, NumberType
, and Description
. There are a number of objects in my domain that have collections of phone numbers.
In the database (SQL 2008), these values are held in different tables. So I might have Customers
and CustomerPhoneNumbers
as well as Vendors
and VendorPhoneNumbers
. The phone number tables are identical except for the foreign key that relates them to their parent.
The problem is that I would like to use a simple PhoneNumber
value type without having to create CustomerPhoneNumber
and VendorPhoneNumber
types that have properties that relate them to their parent type, and I don't know how to accomplish that in NHibernate. Is this possible or do I need to change my domain objects to more closely match the underlying database schema?
UPDATE: A little more info
It looks like I'm having trouble even getting the basic map to work for retrieval. Here's a simplified example of what I have:
public class CustomerMap : ClassMap<Customer>
{
public CustomerMap()
{
Table("Customers");
Id(x => x.Id);
Map(x => x.Name);
Component(x => x.Address);
HasMany(x => x.PhoneNumbers)
.KeyColumn("CustomerId")
.Cascade.All()
.Table("CustomerPhoneNumbers");
}
}
public class PhoneNumberMap : ClassMap<PhoneNumber>
{
public PhoneNumberMap()
{
Id(x => x.Id);
Map(x => x.Number, "PhoneNumber");
Map(x => x.PhoneNumberType);
Map(x => x.Description);
}
}
It looks like the SQL isn't being generated properly. When I try to look into a customer's PhoneNumbers list, I get an ADO error that reveals that NHibernate is looking for a table named PhoneNumber
.
It seems like it's not picking up the Table("CustomerPhoneNumbers")
part and is defaulting to a table named the same thing as the object. Yet I can't specify a Table in the PhoneNumberMap, because it'll be different depending on which aggregate root we're pulling for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用实体名称映射属性将单个对象 PhoneNumber 映射到多个表。抱歉,没有示例代码 - 我不使用 Fluent,所以我无法在这方面提供帮助。如果有帮助的话,我可以发布示例 hbm 映射。
实体名称基本上全面取代了类名称。当您使用实体名称时,每当您通过会话使用对象时,您还必须修改会话以接受参数来指定实体名称。
文档位于:
http://docs .jboss.org/hibernate/core/3.2/reference/en/html/mapping.html#mapping-entityname
编辑添加了 hbm 示例。
这将单个对象 PhoneNumber 映射到多个表
然后,要调用 PhoneNumber 上的更新,您可以将会话语法修改为以下内容,以便 nhibernate 知道要使用哪个表:
您必须弄清楚 Fluent 是否支持此功能,如果支持,如何支持去做它。如果没有,您始终可以使用 hbm 文件作为 PhoneNumber 对象。
You can map the single object PhoneNumber to multiple tables using the entity-name mapping attribute. Sorry, no sample code - I do not use Fluent so I cannot help with that aspect. I could post sample hbm mappings if that would be helpful.
Entity-name basically replaces class name across the board. When you use entity-name you will also have to modify your session to accept a parameter to specify the entity name whenever you work with the objects via the session.
Documentation is here:
http://docs.jboss.org/hibernate/core/3.2/reference/en/html/mapping.html#mapping-entityname
EDIT Added hbm samples.
This maps a single object PhoneNumber to multiple tables
Then to call for example an update on a PhoneNumber you modify the session syntax to the following so nhibernate knows which table to use:
You will have to figure out if Fluent even supports this, if so how to do it. If not you can always use hbm files for the PhoneNumber object.