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.  [ 3 posts ] 
Author Message
 Post subject: Exception when saving object graph
PostPosted: Mon Sep 08, 2008 11:53 am 
Newbie

Joined: Mon Mar 03, 2008 1:44 pm
Posts: 12
Location: Vancouver, BC
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;
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 1:58 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You have specified unsaved-value="-1" so hibernate considers objects with id 0 as saved. Do you set an initial id of -1 to new objects ? I recommend that you stay with the default here which is an unsaved value of 0 and define the identity column accordingly.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 2:27 pm 
Newbie

Joined: Mon Mar 03, 2008 1:44 pm
Posts: 12
Location: Vancouver, BC
wolli wrote:
You have specified unsaved-value="-1" so hibernate considers objects with id 0 as saved. Do you set an initial id of -1 to new objects ? I recommend that you stay with the default here which is an unsaved value of 0 and define the identity column accordingly.


Thank you. Yes, that fixed it! I was under the (mistaken) impression that I needed to provide a non-zero unsaved value as a default. With the value changed to 0, the object graph is saving correctly.


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