使用未映射列映射组合键 [英] Map composite key with unmapped column
问题描述
如何映射组合键,如果它的一部分没有映射到我的实体中?
示例:
我有一个包含列的表 ITEMDELIVERY
:
(PK)
DELIVERY_DATE
b
我有一个包含列的表 ITEMDELIVERYDETAIL
:
ITEMDELIVERY_ID (PK)
ITEMDELIVERY_ID $ c $ (FK)
PARTITIONDATE
(PK,FK)正如你所看到的,两个表中都有一个复合键, ITEMDELIVERYDETAIL
有一个复合外键给 ITEMDELIVERY
。
在我的域模型 但现在,我如何映射组合键 我尝试了以下操作, 我得到以下错误: NHibernate.PropertyNotFoundException:未能找到类属性 'DeliveryDate' 的吸气剂 'REM.Domain.NHibernate.ItemDeliveryDetail' 更新: 我想我找到了解决方案: 更改组合键的声明为: 这有副作用, code> ItemDeliveryDetail 不能级联保存 然而,我想知道一件事情: 你有一个传统的数据库,并将永远不会生成与SchemaExport,所以没关系,如果NH认为它有3个PK列或1。 在看你的问题带顺序的复合键,如果你的ID是独一无二的,那么它会更好 即使在db中,PK跨越两列 更新:您的发票示例中的棘手问题。在连接中有第二列(作为查询优化器应该添加到连接的地方)的一种方法。 How do I map a composite key, if part of it is not mapped in my entity? Example: I have a table I have a table As you can see, there is a composite key in both tables and There exists no property PartitionDate in my domain model But now, how do I map the composite key in I tried the following, but that doesn't work: I get the following error: NHibernate.PropertyNotFoundException: Could not find a getter for property 'DeliveryDate' in class 'REM.Domain.NHibernate.ItemDeliveryDetail'
UPDATE: I think I found the solution: Change the declaration of the composite key to this: This has the side effect that saving an However, I wonder about one thing: it looks like you have a legacy db and will never generate it with SchemaExport, so it doesnt matter if NH thinks it has 3 PK columns or 1. after looking at your question Composite key with sequence and if your Id is unique alone then it would be better to even if in the db the PK spans both columns Update: tricky thing in your example of Invoice. There is a hack to have the second column in the join (as a where which the query optimizer should add to the join)
这篇关于使用未映射列映射组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ItemDeliveryDetail
中不存在属性PartitionDate,映射到列 PARTITIONDATE
(请参阅此处和
ITEMDELIVERYDETAIL
?
$ b
mapping.CompositeId()。KeyProperty(x => x.Id,ITEMDELIVERYDETAIL_ID)
.KeyProperty(x => x.ItemDelivery.DeliveryDate,
PARTITIONDATE);
ItemDeliveryDetail 的映射中删除对
ItemDelivery
code>: mapping.References(x => x.ItemDelivery);. Columns(ITEMDELIVERY_ID,PARTITIONDATE);
mapping.CompositeId()。KeyProperty x => x.Id,ITEMDELIVERYDETAIL_ID)
.KeyReference(x => x.ItemDelivery,ITEMDELIVERY_ID,
PARTITIONDATE);
ItemDelivery
。它需要预先保存。
这会创建一个三列PK吗?如果是这样,如何避免它,只需要创建一个两列所需的PK?
mapping.Id(x => x.Id).GeneratedBy.SequenceIdentity(SQ_TRANSFORM_ITEMDEL_IDDID);
mapping.Reference(x => x.ItemDelivery).Columns(ITEMDELIVERY_ID,PARTITIONDATE);
HasManyToMany(U => u.ItemDeliveryDetails)
。表( INVOICEITEM_IDD)
.ChildWhere( PARTITIONDATE = nhGeneratedAliasForINVOICEITEM_IDD.PARTITIONDATE);
ITEMDELIVERY
containing the columns:
ITEMDELIVERY_ID
(PK)DELIVERY_DATE
(PK)ITEMDELIVERYDETAIL
containing the columns:
ITEMDELIVERYDETAIL_ID
(PK)ITEMDELIVERY_ID
(FK)PARTITIONDATE
(PK, FK)ITEMDELIVERYDETAIL
has a "composite" foreign key to ITEMDELIVERY
.ItemDeliveryDetail
that could be mapped to the column PARTITIONDATE
(see here and here for the reasons).ITEMDELIVERYDETAIL
?mapping.CompositeId().KeyProperty(x => x.Id, "ITEMDELIVERYDETAIL_ID")
.KeyProperty(x => x.ItemDelivery.DeliveryDate,
"PARTITIONDATE");
ItemDelivery
from the mapping of ItemDeliveryDetail
: mapping.References(x => x.ItemDelivery);.Columns("ITEMDELIVERY_ID", "PARTITIONDATE");
mapping.CompositeId().KeyProperty(x => x.Id, "ITEMDELIVERYDETAIL_ID")
.KeyReference(x => x.ItemDelivery, "ITEMDELIVERY_ID",
"PARTITIONDATE");
ItemDeliveryDetail
can't cascade save an ItemDelivery
. It needs to be saved upfront.
Will this create a PK with three columns? If so, how to avoid it and only create a PK for the two columns needed?mapping.Id(x => x.Id).GeneratedBy.SequenceIdentity("SQ_TRANSFORM_ITEMDEL_IDDID");
mapping.Reference(x => x.ItemDelivery).Columns("ITEMDELIVERY_ID", "PARTITIONDATE");
HasManyToMany(u => u.ItemDeliveryDetails)
.Table("INVOICEITEM_IDD")
.ChildWhere("PARTITIONDATE = nhGeneratedAliasForINVOICEITEM_IDD.PARTITIONDATE");