[英]nhibernate : One to One mapping
I have the following map. 我有以下地图。 I wish to map BasketItem to the class "Product".
我希望将BasketItem映射到“Product”类。 So basically when i iterate thru the basket i can get the product name
所以基本上当我通过篮子迭代时我可以获得产品名称
<class name="BasketItem" table="User_Current_Basket">
<id name="Id" type="Int32" column="Id" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="ProductId" column="Item_ID" type="Int32"/>
<one-to-one name="Product"
class="Product"></one-to-one>
</class>
How do specifiy that product should match BasketItem.ProductId with Product.Id 如何指定该产品应该与Product.Id匹配BasketItem.ProductId
Also i've read that i should avoid one-to-one and just use one-to-many? 另外我读过我应该避免一对一,只使用一对多? If i was to do that how do i ensure i just get one product and not a collection.
如果我这样做,我如何确保我只获得一个产品而不是一个集合。
// EDIT //编辑
select * from BasketItem
inner join Products on BasketItem.Item_ID = Products.Item_ID
How do specifiy that product should match BasketItem.ProductId with Product.Id
如何指定该产品应该与Product.Id匹配BasketItem.ProductId
Your BasketItem
should hold a Product
at the object level, not the ProductId
. 您的
BasketItem
应该在对象级别持有Product
,而不是ProductId
。 And the mapping should be: 映射应该是:
<class name="BasketItem" table="User_Current_Basket">
<id name="Id" type="Int32" column="Id" unsaved-value="0">
<generator class="identity"/>
</id>
<one-to-one name="Product" class="Product"/>
</class>
Also i've read that i should avoid one-to-one and just use one-to-many?
另外我读过我应该避免一对一,只使用一对多? If i was to do that how do i ensure i just get one product and not a collection.
如果我这样做,我如何确保我只获得一个产品而不是一个集合。
If you want to be able to lazy-load the Product
, prefer a "fake" many-to-one: 如果您希望能够延迟加载
Product
,请选择“假”多对一:
<class name="BasketItem" table="User_Current_Basket">
<id name="Id" type="Int32" column="Id" unsaved-value="0">
<generator class="identity"/>
</id>
<many-to-one name="Product" class="Product"/>
</class>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.