views:

25

answers:

1

I have the following map. I wish to map BasketItem to the class "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

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
+3  A: 

How do specifiy that product should match BasketItem.ProductId with Product.Id

Your BasketItem should hold a Product at the object level, not the 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:

<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>
Pascal Thivent
thanks, in regards to the one-to-one. How does "BasketItem" know that it has to match all products on basketItem.ProductId = product.Id. I've added some sql above that might make this clear
frosty
@frosty Well, that's precisely what the mapping is saying and that's precisely the job of NHibernate (you may need to add a `column` attribute in the `one-to-one` relation if you are not using NHibernate's defaults). Did you try?
Pascal Thivent
thanks, i couldn't find the column property on one-to-one. I've set up many-to-one and it seems to work except i think i now have session issued. I've posted a new question about this.
frosty
That's a typo, I meant `many-to-one`. Sorry for that.
Pascal Thivent
@Pascal; I saw a similar answer in another post, and this is something I still haven't quite grokked - that is why a 1-1 object relationship should be mapped as M-1 for Nhib. Could you give a brief explanation of why this makes sense or refer me to a link that does that? Also, if you could please edit your post to reflect the many-to-one mapping. Cheers
Berryl