I am attempting to save an object graph containing the parent object (GLBatch) and 1 associated set (AccountTransactions).
Upon save, the following exception is thrown:
"a different object with the same identifier value was already associated with the session: 0, of class: ORM.Domain.AccountTransaction".
I suspect something in the <set> element of my mapping document is not set correctly, or possible in the child association methods in the parent class.
Thanks for any suggestions on how to fix this.
David Gadd
Database: MS SQL Server
NHibernate: v1.2.0
The id element for each class:
Code:
<id name="Id" column="AccountTransactionID" type="long" unsaved-value="-1" access="field.camelcase-underscore">
<generator class="identity" />
</id>
On the parent class, I am setting the association as follows:
Code:
<many-to-one name="GLBatch" class="GLBatch" column="GLBatchID" not-null="true" access="field.camelcase-underscore" fetch="select"/>
On the child class, I am setting the many-to-one element as follows:
Code:
<set name="AccountTransactions" lazy="true" access="field.camelcase-underscore" cascade="all" inverse="true">
<key column="GLBatchID"/>
<one-to-many class="AccountTransaction"/>
</set>
Finally, my code in the parent class to express the child association is:
Code:
private ISet<AccountTransaction> _accountTransactions = new HashedSet<AccountTransaction>();
public virtual ReadOnlyCollection<AccountTransaction> AccountTransactions
{
get
{
if (_accountTransactions != null)
{
return new ReadOnlyCollection<AccountTransaction>(new List<AccountTransaction>(_accountTransactions));
}
else
{
return null;
}
}
}
public virtual void AddAccountTransaction(AccountTransaction accountTransaction)
{
if (!_accountTransactions.Contains(accountTransaction))
{
accountTransaction.GLBatch = this;
_accountTransactions.Add(accountTransaction);
}
}
public virtual void RemoveAccountTransaction(AccountTransaction accountTransaction)
{
if (_accountTransactions.Contains(accountTransaction))
{
_accountTransactions.Remove(accountTransaction);
accountTransaction.GLBatch = null;
}
}