-->
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.  [ 4 posts ] 
Author Message
 Post subject: Many-to-Many..Please Help!
PostPosted: Wed Aug 24, 2005 4:37 pm 
Newbie

Joined: Sun Jun 19, 2005 12:34 pm
Posts: 6
Hi!

I have been banging my head against this for two days now...
NHibernate throws "PropertyNotFoundException" and noting gets persisted to the db. What is the proper way of doing this? Please post a working solution for loading/saving many-to-many relations using a collection in the two domain objects.

Two classes:
Code:
class User {
int id;
string name;
IList roles;
}

class Role {
int id;
string name
IList users;
}


The database contains three tables: User, Role and UserRole for the many-to-many relationship..

My mappings:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="FirstNHibernate.Domain.User, FirstNHibernate" table="User">
      <id name="ID" column="Id" type="Int32" unsaved-value="0">
         <generator class="native" />
      </id>
      <property name="Name" column="Name" type="String" length="50"/>
      <bag name="Roles" table="UserRole" lazy="false">
         <key column="UserID" />
         <many-to-many column="RoleID" class="FirstNHibernate.Domain.Role, FirstNHibernate"  />
      </bag>
   </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="FirstNHibernate.Domain.Role, FirstNHibernate" table="Role">
      <id name="Id" column="Id" type="Int32" unsaved-value="0">
         <generator class="native" />
      </id>
      <property name="Name" column= "Name" type="String" length="50"/>
      <bag name="Users" table="UserRole" inverse="true" lazy="true">
         <key column="RoleID" />
         <many-to-many column="UserID" class="FirstNHibernate.Domain.User, FirstNHibernate"  />
      </bag>
   </class>
</hibernate-mapping>



Regards
/Oscar


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 6:12 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
I think the problem you are having is that your mapping files are referring to the members of your class without respecting case sensitivity and your mapping files are referring to properties rather than non public fields.

Because you are not exposing the members id, name or roles you need to tell NHibernate that you are using a field rather than a property.

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="FirstNHibernate.Domain.User, FirstNHibernate" table="User">
      <id name="id" column="Id" type="Int32" unsaved-value="0" access="field">
         <generator class="native" />
      </id>
      <property name="name" column="Name" type="String" length="50" access="field/>
      <bag name="roles" table="UserRole" lazy="false" access="field">
         <key column="UserID" />
         <many-to-many column="RoleID" class="FirstNHibernate.Domain.Role, FirstNHibernate"  />
      </bag>
   </class>
</hibernate-mapping>


Notice that I've changed the "name" attribute values to match the case of the members in your class and I've added the "access" attribute to tell NHibernate to look for the private field that matches the one specified in the "name" attribute.

Hope this helps.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 2:42 pm 
Newbie

Joined: Sun Jun 19, 2005 12:34 pm
Posts: 6
Yep! That was it!
Many thanks Rottem!

(of course I


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 6:54 pm 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
No problems. I was a complete noob about 2 months ago and I'm really starting to get the hang of NHibernate now - glad I could help!

Cheers,

Symon.


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