These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: weird lazy initialization issue - collection + inheritance
PostPosted: Wed Jan 24, 2007 9:50 am 
Newbie

Joined: Wed Jan 24, 2007 9:35 am
Posts: 7
I'm using NHibernate 1.2 Beta 3 with recent SQLite, and I have a problem I can't solve (maybe it's a bug of NHibernate ?).

I'm writing a simple library-like app, and the situation is as follows:
Class Book, derives directly from class MediaItem (table-per-subclass mapping strategy).
Class Creator has an ordered collection (list) for Book objects.

I have also a Contact Class, to whom i can borrow MediaItem-s. I'm using a many-to-many association mapped with an additional class: Borrowing.
Thus a Borrowing class has a many-to-one association to MediaItem, and another many-to-one to Contact. I post the mapping, but they should be fine, read the Problem section below to now what to look for.

The mapping part for authors in Book.hbm is:
Code:
  <list name="Authors" table="CREATOR_C_ITEM" lazy="true" cascade="save-update">
      <key column="ITEM_ID"/>
      <index column="ITEM_POSITION"/>
      <many-to-many class="MediaLibrary.People.Creator" column="CREATOR_ID"/>
    </list>


The other end in Creator.hbm is :
Code:
<set name="ItemsCreated" table="CREATOR_C_ITEM" inverse="true">
      <key column="CREATOR_ID"/>
      <many-to-many class="MediaLibrary.Items.MediaItem" column="ITEM_ID"/>
    </set>


Now for the Borrowing.hbm
uniderectional part to MediaItem that is borrowed:
Code:
<many-to-one name="ItemBorrowed" column="ITEM_ID" class="MediaLibrary.Items.MediaItem" not-null="true"/>   


biderectional part to the Borrower of the MediaItem:
Code:
<many-to-one name="Borrower" column="BORROWER_ID" class="Contact" not-null="true"/>


The other end of the association in Contact.hbm is:
Code:
   <set name="Borrowings" inverse="true" cascade="save-update">
      <key column="BORROWER_ID"/>
      <one-to-many class="Borrowing"/>
    </set>


Now the problem:

I'm using lazy initialization everywere, and I'm having weird problems with inheritance.

Let's assume I have a collection of books (Book objects) associated properly with their author and some borrowings (also, only Book objects).


If i try to invoke the following code:

Code:
Main(){

Contact contact = PM.Get<Contact>(1);
Creator creator = PM.Get<Creator>(1);

//LIST THE BOOKS by AUTHOR
foreach (MediaItem item in creator.ItemsCreated)
   Console.Writeline((item as Book));
   
//LIST THE BORROWED BOOKS
foreach (Borrowing borrowing in contact.Borrowings)
   Console.Writeline((borrowing.ItemBorrowed as Book));
   
   }


Everything is fine, i get a nice list corresponding to the data stored.

BUT if I do it the oposite way:

Code:
Main(){

Contact contact = PM.Get<Contact>(1);
Creator creator = PM.Get<Creator>(1);

//FIRST LIST THE BORROWED BOOKS
foreach (Borrowing borrowing in contact.Borrowings)
   Console.Writeline((borrowing.ItemBorrowed as Book));
   
//LIST THE BOOKS by AUTHOR
foreach (MediaItem item in creator.ItemsCreated)
   Console.Writeline((item as Book));
   
   }


All I get is a list of nulls as the corresponding object cannot be cast back to Book.


I've snooped around using the VS2005 debugger, and in the first case, I can see Book objects in the internal collection for either Creator or Borrowing.
In the second one, the collections contain: ((CProxyTypeMediaLibrary_ItemsMediaItemItems_NHibernate_ProxyINHibernateProxy1) objects, which I can at best cast to MediaItem, not Book.


Anyone have ANY idea what is going on? It is as if lazy initialization filled the Session cache with improper objects :/


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 1:50 pm 
Newbie

Joined: Wed Jan 24, 2007 9:35 am
Posts: 7
Seriously, nobody?

I've managed to temporarily "solve" the problem by simple loading one item at the very beginning, but that's not what one would consider a fix.

If at least someone could tell me if it's unlikely a bug, I'd dig deeper...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.