开发者

Trying to map several bags with the same table - failed to lazily initialize a collection of role exception

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">开发者_Go百科;
  <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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜