I've just started investigating NHibernate in the last few days and have worked out almost all of my issues until now.
I have created a many to many relationship between "accounts" and "contacts" using a joining table.
My code successfully saves both objects to my database without throwing any errors, but it does not make an entry in the join table. So, just to clarify, nhibernate stores the account in tblAccounts, and it stores the contact in tblContacts, but it does not make an entry in the tblContactAccountJoin table so I lose the relationship.
I'm assuming that I have missed something (probably very simple) in my mapping XML but can not figure out what. Any help would be much appreciated.
Hibernate version: 1.0.2
Mapping documents:
Code:
<class name="portal.Account, portal" table="tblAccounts">
<id name="AccountID" column="accountID" >
<generator class="identity" />
</id>
<property name="AccountName" column="accountName" />
<bag name="Contacts" table="tblContactAccountJoin" cascade="all" inverse="false" lazy="false">
<key column="accountID" />
<many-to-many column="contactID" class="portal.Contact, portal" />
</bag>
</class>
<class name="portal.Contact, portal" table="tblContacts" discriminator-value="?">
<id name="ContactID" column="contactID" unsaved-value="0">
<generator class="identity" />
</id>
<bag name="Accounts" table="tblContactAccountJoin" cascade="all" inverse="true" lazy="false">
<key column="contactID" />
<many-to-many column="accountID" class="portal.Account, portal" />
</bag>
<property name="NameFirst" column="nameLast" type="string" length="50" />
<property name="NameLast" column="nameFirst" type="string" length="50" />
<property name="Title" column="title" type="string" length="50" />
Code between sessionFactory.openSession() and session.close():
Code:
Account newAccount = new Account();
newAccount.AccountName = accountName.Text;
Contact myContact = new Contact();
myContact.NameFirst = "ian";
myContact.NameLast = "emerick";
myContact.Accounts.Add(newAccount);
newAccount.Contacts.Add(myContact);
session.SaveOrUpdate(newAccount);
Class Definitions:
Quote:
public class Account
{
private int accountID;
private string accountName;
private IList contacts = new ArrayList(); //Many-to-Many Relationship
public Account()
{
}
public int AccountID
{
get { return accountID; }
set { accountID = value; }
}
public string AccountName
{
get { return accountName; }
set { accountName = value; }
}
public IList Contacts
{
get { return this.contacts;}
set { this.contacts = value;}
}
}
public class Contact
{
private int contactID;
private string nameFirst;
private string nameLast;
private string title;
private IList accounts = new ArrayList();
public Contact()
{
}
public int ContactID
{
get { return contactID; }
set { contactID = value; }
}
public string Title
{
get { return title; }
set { title = value; }
}
public string NameFirst
{
get { return nameFirst; }
set { nameFirst = value; }
}
public string NameLast
{
get { return nameLast; }
set { nameLast = value; }
}
public IList Accounts
{
get { return this.accounts; }
set { this.accounts = value; }
}
}
Name and version of the database you are using: MSSQL 2000