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.  [ 5 posts ] 
Author Message
 Post subject: many-to-many issue, please i'm desperate
PostPosted: Sun Jun 07, 2009 8:15 pm 
Newbie

Joined: Sun Jun 07, 2009 8:03 pm
Posts: 3
Hi.

I found many articles around the net and even saw a post here about this issue but still i keep on getting the exception:
NHibernate.MappingException : An association from the table ItemsCategories refers to an unmapped class: Entities.Category

Here are my files (Each class resides in a separate file, but in same namespace):

Code:
namespace DOM.Entities
{
    public class Item
    {
          public int Id { get; set; }
          public string Name { get; set; }
          .... etc.
          public IList<Category> Categories { get; set; } // This is the new addition
    }

    public class Category
    {
          public int Id { get; set; }
          public string Name { get; set; }
          public IList<Item> Items; { get; set; }
    }



Here are the mappings:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="DOM"
                   namespace="DOM.Entities">

  <class name="Item" table="Items" lazy="true" abstract="false">
   
    <id name="Id" column="ItemId" type="Int32">
      <generator class="native"/>
    </id>
   
    <property name="Title" type="string"/>
<!--   ... etc.   -->

    <bag name="Categories" table="ItemCategories" cascade="all">
      <key column="ItemId"/>
      <many-to-many class="Category" column="CategoryId" />
    </bag>

  </class>
 
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="DOM"
                   namespace="DOM.Entities">

  <class name="Category" table="Categories" lazy="true" >

    <id name="Id" column="CategoryId" type="Int32">
      <generator class="native"/>
    </id>

    <property name="Name" type="string"/>

    <bag name="Items" table="ItemCategories" cascade="all">
      <key column="CategoryId"/>
      <many-to-many class="Item" column="ItemId" />
    </bag>

  </class>
 
</hibernate-mapping>


The Item class didn't have categories before, that's a new feature and it matches a many-to-many collection.

Can anybody please help with that exception?
Do I need to define a class ItemCategories that would map to that table? I'm pretty lost.....

Thanks ahead!
Zvika


Top
 Profile  
 
 Post subject: Re: many-to-many issue, please i'm desperate
PostPosted: Mon Jun 08, 2009 2:44 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The mapping looks fine except that you have to declare one end of the assocation as inverse="true" (meaning the opposite end is the owner). The exception you get normally means that hibernate can't find the mapping file. Check if build action is set to "Embedded Ressource".

Remove the association from both mapping files and check if you can load entities of both types. If that's working, add the associations again.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: many-to-many issue, please i'm desperate
PostPosted: Mon Jun 08, 2009 3:18 am 
Newbie

Joined: Sun Jun 07, 2009 8:03 pm
Posts: 3
wolli wrote:
The mapping looks fine except that you have to declare one end of the assocation as inverse="true" (meaning the opposite end is the owner). The exception you get normally means that hibernate can't find the mapping file. Check if build action is set to "Embedded Ressource".

Remove the association from both mapping files and check if you can load entities of both types. If that's working, add the associations again.


First of all THANKS A LOT!!! I spent a few hours without understand why Category is unknown to the mapping and forgot about that embedded resource issue...
Now the schema is generated, i'll have to dive into it later since i have a long working day ahead, if i define the set in the category side as inverse="true" would that be good? does it mean that the Item is the "stronger" entity of that set?

Oh, and one more thing... if i want to define this as a set (no worries about ordering, and want only one occurances of each element) what is the easiest/best way? and is there any non-nhibernate class to do it with? (i saw ISet<> and HashedSet<> but that forces me to use a reference to nhibernate's iesi dll and that would "ruin" my layers of abstraction since it will rely on NHibernate)

And thanks again!
Zvika


Last edited by zvoykish on Tue Jun 09, 2009 7:46 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: many-to-many issue, please i'm desperate
PostPosted: Mon Jun 08, 2009 5:19 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Check this doc for more infos about parent/child mappings:

http://nhforge.org/doc/nh/en/index.html#example-parentchild

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: many-to-many issue, please i'm desperate
PostPosted: Tue Jun 09, 2009 7:45 am 
Newbie

Joined: Sun Jun 07, 2009 8:03 pm
Posts: 3
OK...

I managed with the class/mapping definitions and also created a category repository, but now i'm facing a query...

I want to implement:
IList<Item> GetItemsMatchingCategories (IList<Category> categories, bool matchAll)

that will return all items that contains at least one of the categories (or all if matchAll is true).
Can it be done with DetachedCriteria in the ItemRepository?

I usually use:
DetachedCriteria.For(typeof(Item)).Add(Expression.Eq(string, object))...

Any help would be appreciated...
Thanks ahead for all the help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.