views:

38

answers:

1

I have a problem with mapping in nhibernate. I am using nhibernate 2.2 version.

Seems like the problem is in mapping but I am not sure this is the cause. Anyway, I have two tables which I would like to map. I created a hbm file for first table and a data transfer object too. All columns were mapped and everything works fine here.

But, now I want to add three bags to this class, which will point to the same table, my second table which I'd like to connect with. I created bags and mapped everything but when I am retrieving my data only one of these bags is filled, and the other ones are left empty, and I get an error "failed to lazily initialize a collection of role: com.organic.mitsu.hib.ModelContent.options - no session or session was closed". And I am 100% sure that my data in database are good. When I remove two bags from my mapping everything works fine, with only one bag left. Here is the hbm file:

<class name="MyFirstClass" table="MyFirstTable">
<id name="ID">
  <generator class="native" />
</id>
<property name="ItemOne" />
<property name="ItemTwo" />
<property name="ItemThree" />
<property name="ItemFour" />

<bag name="FirstItems" table="MySecondTable">
  <key column="ItemID" property-ref="ItemOne"/>
  <one-to-many class="Items" not-found="ignore"/>
</bag>

<bag name="SecondItems" table="MySecondTable">
  <key column="ItemID" property-ref="ItemTwo"/>
  <one-to-many class="Items" not-found="ignore"/>
</bag>

<bag name="ThirdItems" table="MySecondTable">
  <key column="ItemID" property-ref="ItemThree"/>
  <one-to-many class="Items" not-found="ignore"/>
</bag>

How should I solve the problem? Is this even possible to do it like this?

And here is the mapping for the MySecondTable:

<class name="Item" table="MySecondTable">
<id name="ID">
  <generator class="assigned" />
</id>
<property name="ItemID" />
<property name="Language" />
<property name="Value" />

Actually, the original thing that I was trying to map is with composite element and without the mapping for MySecondTable. I only have a dto class Item, with ItemID and Value columns. I got the same error and the mapping looks like this:

<class name="MyFirstClass" table="MyFirstTable">
<id name="ID">
  <generator class="native" />
</id>
<property name="FirstItem" />
<property name="SecondItem" />
<property name="ThirdItem" />

<bag name="FirstItemNames" table="MySecondTable">
  <key column="ItemID" property-ref="FirstItem"/>
  <composite-element class="Item">
    <property name="Value" />
  </composite-element>
</bag>

<bag name="SecondItemNames" table="MySecondTable">
  <key column="ItemID" property-ref="SecondItem"/>
  <composite-element class="Item">
    <property name="Value" />
  </composite-element>
</bag>

<bag name="ThirdItemNames" table="MySecondTable">
  <key column="ItemID" property-ref="ThirdItem"/>
  <composite-element class="Item">
    <property name="Value" />
  </composite-element>
</bag>

+1  A: 

Sounds like the SecondItems and ThirdItems are are being fetched lazily after the session was closed, which is not allowed. You need to either force the fetching while the session is active or change the mappings so that lazy fetch (the default) is turned off.

See here for more details.

David Lynch
I tried with force the fetching and didn't solve the problem. I forgot to say that my ThirdItems collection is only filled with data and First and Second are empty.
Behemoth
Are you using a Transaction? How\where did you force the fetching?
David Lynch
I am using a transaction when retrieving data. I tried to force the fetching in mapping file.
Behemoth
Can you for the fetch by accessing the properties within the transaction? What is the mapping for Items? Does it also have relations?
David Lynch
Items doesn't have a mapping. Just a class which represents the table's columns. Is that wrong?I found another way to hack this, but I'd like to know where was I doing wrong and if it is possible do it like this, with three bags referencing to the same table.
Behemoth
I guess it depends on what the mappings are for MySecondTable. Can you add them?
David Lynch
I added the mapping for MySecondTable in original post.
Behemoth