【发布时间】:2011-12-27 21:26:35
【问题描述】:
我正在尝试弄清楚如何为下面的代码示例中显示的类 BOM 设置休眠映射。我遇到问题的部分是如何映射属性BOM.Components,以便映射等同于我在下面发布的 SQL。
到目前为止,这是我的 BOM 映射: :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Inventory.Core"
namespace="Inventory.Core.Entities">
<class name="BOM" table="bom">
<id name="Id" column="bom_id">
<generator class="hilo" />
</id>
<many-to-one name="Product" class="Material" column="mtrl_id" foreign-key="fk_BOM_Material" unique="true" not-null="true" />
<map name="Components" table="bom_cmpnnts">
<key column="bom_id" foreign-key="fk_BOMComponent_BOM" not-null="true" />
</map>
</class>
</hibernate-mapping>
更新:
这是 BOMComponent 的映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Inventory.Core"
namespace="Inventory.Core.Entities">
<class name="BOMComponent" table="bom_cmpnnt">
<id name="Id" column="bom_cmpnnt_id">
<generator class="hilo" />
</id>
<version name="Version" column="bom_cmpnnt_vrsn" />
<many-to-one name="Component" class="Material" column="mtrl_id" foreign-key="fk_BOMComponent_Material" not-null="true" />
<property name="Unit" column="bom_cmpnnt_unt" not-null="true" />
<property name="Quantity" column="bom_cmpnnt_qntty" not-null="true" />
<many-to-one name="User" class="User" column="upsrt_usr_id" foreign-key="fk_BOMComponent_User" lazy="false" not-null="true" />
<property name="Timestamp" column="upsrt_dttm" generated="always" />
</class>
</hibernate-mapping>
类:
public class BOM
{
int Id { get; set; }
Material Product { get; set; }
IDictionary<int, BOMComponent> Components { get; set; }
// Key = BOMComponent.Material.Id
// for each BOM, materials should be unique
//other properties ...
}
public class BOMComponent
{
int Id { get; set; }
Material Material { get; set; }
//other properties ...
}
public class Material
{
int Id { get; set; }
//other properties
}
这就是 SQL 的样子:
delimiter $$
CREATE TABLE `mtrl` (
`mtrl_id` int(11) NOT NULL,
PRIMARY KEY (`mtrl_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
delimiter $$
CREATE TABLE `bom` (
`bom_id` int(11) NOT NULL,
`mtrl_id` int(11) NOT NULL,
PRIMARY KEY (`bom_id`),
UNIQUE KEY `mtrl_id_UNIQUE` (`mtrl_id`),
KEY `fk_BOM_Material` (`mtrl_id`),
CONSTRAINT `fk_BOM_Material` FOREIGN KEY (`mtrl_id`) REFERENCES `mtrl` (`mtrl_id`) ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
delimiter $$
CREATE TABLE `bom_cmpnnt` (
`bom_cmpnnt_id` int(11) NOT NULL,
`bom_id` int(11) NOT NULL,
`mtrl_id` int(11) NOT NULL,
PRIMARY KEY (`bom_cmpnnt_id`),
UNIQUE KEY `IX_BOMComponent_UQ` (`bom_id`,`mtrl_id`),
KEY `fk_BOMComponent_BOM` (`bom_id`),
KEY `fk_BOMComponent_Material` (`mtrl_id`),
CONSTRAINT `fk_BOMComponent_BOM` FOREIGN KEY (`bom_id`) REFERENCES `bom` (`bom_id`) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT `fk_BOMComponent_Material` FOREIGN KEY (`mtrl_id`) REFERENCES `mtrl` (`mtrl_id`) ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
【问题讨论】:
-
@J Cooper,将
Dictionary<Value,Object>视为IEnumerable<KeyValuePair<Value,Object>>对Value具有唯一性约束可能会对您有所帮助 -
@smartcaveman 仍然没有得到它,我之前遇到过那个链接......这有点倒退到我正在做的事情,因为他使用复杂类型的键和简单的类型价值。我仍然迷路(对 NHibernate 没有经验)。我将发布我正在尝试的新映射 - 虽然我收到错误
-
@smartcaveman - 没关系 - 新的映射有效!
标签: c# mysql nhibernate