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: Many to many reation doest work
PostPosted: Fri Sep 25, 2009 3:15 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 6:44 am
Posts: 24
I have been working at Nhibernate and Many to many relation is not working properly.
i am not getting idea how to save value in a junction table.

My code is Below
======================================My User Class==============================
public class Users
{
protected int _userid;
protected string _firstname;
protected string _lastname;
private ISet _Interest = new HashedSet();
public virtual void AddCategory(Interests interest)
{
//interest.User.Add(this);
interests.Add(interest);

}
}

=================================my User XML=============================
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DTO" namespace="Business.Entities">
<class name="Business.Entities.Users" table="Users" lazy="true">
<id name="UserId" column="UserId" type="int">
<generator class="native" />
</id>
<property type="string" length="50" name="FirstName" column="[FirstName]" />
<property type="string" length="50" name="LastName" column="[LastName]" />
<property type="Boolean" name="MaleInd" column="[MaleInd]" />
<property type="string" length="20" name="ContactNumber" column="[ContactNumber]" />
<property type="string" length="50" name="Email" column="[Email]" />
<property type="DateTime" name="ModificationDate" column="[ModificationDate]" />
<set name="interests" inverse="false" table="UserInterests" lazy="true" cascade="all">
<key column="UserId" />
<many-to-many class="Business.Entities.Interests">
<column name="InterestID" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
=================================my interest class=============================

public class Interests
{
private int _interestid;
private string _interestdesc;
//private ISet user = new HashedSet();
private Users user;
}

}

=================================my interest XML=============================
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Business.Entities" assembly="DTO">
<class name="Business.Entities.Interests" table="Interests" lazy="true">
<id name="InterestId" column="InterestId" type="int">
<generator class="native" />
</id>
<property type="string" not-null="true" length="50" name="InterestDesc" column="[InterestDesc]" />
<set name="User" inverse="false" table="UserInterests" lazy="true" cascade="all">
<key column="InterestID" />
<many-to-many class="Business.Entities.Users">
<column name="UserId" />
</many-to-many>
</set>
</class>
</hibernate-mapping>

=================================my DAL====================================
Interests interest = new Interests();
interest.InterestDesc = "I like swimming";
Users user = (Users)session.Load(typeof(Users), 6);
interest.User=user;
user.interests.Add(interest);
session.Save(user);
session.Flush();



Now i Dont know how to add data in many to many relation database.
Can any body tell me how to solve it ... or some body could refer me to an example which should work too.

I am stuck here since 9 days... i mean 9 working days... :(


Top
 Profile  
 
 Post subject: Re: Many to many reation doest work
PostPosted: Fri Sep 25, 2009 8:23 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Code:
This should work:

private ISet _Interest = new HashedSet();
public virtual void AddCategory(Interests interest) {
            interest.User.Add(this);
            interests.Add(interest);
}

    <set name="interests" inverse="true" table="UserInterests" lazy="true" cascade="all">
      <key column="UserId" />
      <many-to-many class="Business.Entities.Interests">
        <column name="InterestID" />
      </many-to-many>
    </set>

private ISet user = new HashedSet();

   <set name="User" table="UserInterests" lazy="true" cascade="all">
      <key column="InterestID" />
      <many-to-many class="Business.Entities.Users">
        <column name="UserId" />
      </many-to-many>
    </set>


Interests interest = new Interests();
interest.InterestDesc = "I like swimming";
Users user = (Users)session.Load(typeof(Users), 6);
user.interests.Add(interest);
session.SaveOrUpdate(user);  // or just update - save would generate a copy !
session.Flush();


Two thinks for you to note:

- session.Save() on a perstsited object will give you a copy of the object or an exception with a primary key violation. Depends in the id generator

- If you have a bi-directional assocaition, one end has to be inverse

_________________
--Wolfgang


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.