gpt4 book ai didi

c# - NHibernate - 如何映射 dictionary

转载 作者:行者123 更新时间:2023-11-29 03:11:12 25 4
gpt4 key购买 nike

我正在尝试弄清楚如何为下面的代码示例中所示的类 BOM 设置 nhibernate 映射。我遇到问题的部分是如何映射属性 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$$

最佳答案

我不想回答我自己的问题,但我通过使用这两个映射让它按照我想要的方式工作:

<?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>
<version name="Version" column="bom_vrsn" />
<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_cmpnnt">
<key column="bom_id" foreign-key="fk_BOMComponent_BOM" not-null="true" />
<index column="mtrl_id" type="System.Int32" />
<composite-element class="BOMComponent" />
</map>
<many-to-one name="User" class="User" column="upsrt_usr_id" foreign-key="fk_BOM_User" lazy="false" not-null="true" />
<property name="Timestamp" column="upsrt_dttm" generated="always" />
</class>
</hibernate-mapping>

<?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>

关于c# - NHibernate - 如何映射 dictionary<value, object>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8109174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com