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 :/