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