[英]nhibernate : One to One mapping
我有以下地图。 我希望将BasketItem映射到“Product”类。 所以基本上当我通过篮子迭代时我可以获得产品名称
<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>
如何指定该产品应该与Product.Id匹配BasketItem.ProductId
另外我读过我应该避免一对一,只使用一对多? 如果我这样做,我如何确保我只获得一个产品而不是一个集合。
//编辑
select * from BasketItem
inner join Products on BasketItem.Item_ID = Products.Item_ID
如何指定该产品应该与Product.Id匹配BasketItem.ProductId
您的BasketItem
应该在对象级别持有Product
,而不是ProductId
。 映射应该是:
<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>
另外我读过我应该避免一对一,只使用一对多? 如果我这样做,我如何确保我只获得一个产品而不是一个集合。
如果您希望能够延迟加载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.