-->
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.  [ 7 posts ] 
Author Message
 Post subject: many-to-many collection not updating
PostPosted: Thu Jun 16, 2005 9:11 pm 
Hello,

I'm having trouble making any changes to my many-to-many collection. I get a "object already associated with session" message but nothing happens when I do Update() ... I also tried Save() and SaveOrUpdate() but it doensn't want to do it.
When I make changes to my joining table directly in the database I can see that the collections are correctly populated ... so reading works, I just can't get it to write. This is my code:

Mapping file:
Code:
...
<bag name="Keywords" table="KeywordsFiles" cascade="none" access="nosetter.camelcase" lazy="true" inverse="false">
<key column="FileID"/>
<many-to-many column="KeywordID" class="Bearch.DataEntities.Keyword, Bearch" />
</bag>
...
...
<bag name="Files" table="KeywordsFiles" cascade="none" access="nosetter.camelcase" lazy="true" inverse="true">
<key column="KeywordID" />
<many-to-many column="FileID" class="Bearch.DataEntities.File, Bearch" />
</bag>
...


File.cs
Code:
public IList Keywords
{
get   {   return keywords; }
set   {   keywords = value; }
}


Keyword.cs
Code:
public IList Files
{
get   {   return files; }
set   {   files= value; }
}


Client code
Code:

IList files = session.CreateCriteria(typeof(File)).List();
IList keywords = session.CreateCriteria(typeof(Keyword)).List();

//Just take the 1st file and 1st keyword and associate them
File f = (File)files[0];
Keyword k = (Keyword)keywords[0];

//I'm adding both sides to each other to make sure
f.Keywords.Add(k);
k.Files.Add(f);

//I update both but both give the same log message (see below)
session.Update(f);
session.Update(k);


Log
Code:
2005-06-16 18:58:14,308 [3588] DEBUG NHibernate.Impl.SessionImpl [] <> - object already associated with session
2005-06-16 18:58:14,308 [3588] DEBUG NHibernate.Impl.SessionImpl [] <> - object already associated with session



I'm out of ideas about what could be wrong.. I realize that calling the update on the class that has inverse=true will result in no update, but I'm calling both here (and one of them is inverse=false) so why is it not doing the update? I also tried doing lazy=false but that didn't help much either. I originally copied this code from one sample - it didn't have a setter method on the Keywords and Files properties but instead a new ArrayList() initialization of the private variable ... but having it like that was no better than the way I have it now.

What am I missing here?

Thank you,
Tomas


Top
  
 
 Post subject:
PostPosted: Fri Jun 17, 2005 1:52 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
i am pretty sure you don't want to have "cascade="none"" in there. that is telling hibernate to ignore any statements (update, save, delete, etc). try setting cascace="save-update"

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 17, 2005 9:36 pm 
Thanks, before I had cascade="all" and that didnt' work ... and unfortunately "save-update" didnt' work either. I get the same notification. Any other ideas?

Thanks,
Tomas


Top
  
 
 Post subject: Got it figured out!
PostPosted: Fri Jun 17, 2005 10:57 pm 
Newbie

Joined: Fri Jun 17, 2005 9:38 pm
Posts: 2
Location: Calgary, Canada
I finally figured it out ... I was missing transaction.commit ... I would like to say that I forgot but in reality I had it like this on purpose. I have written one simple application in 0.7 and none of my saves had transactions around them and it works just fine. That's why got in the bad habit of not using them.

So how does it work? I started to think that transactions are optional and used only if you want to have the choice of rolling back. (that even if I don't use transactions changes will be written when I call session.Close())

But in this case it doesn't seem to be so.... was there a change between 0.7 and 0.8.4?

Thank you!
Tomas


Top
 Profile  
 
 Post subject: Re: Got it figured out!
PostPosted: Sat Jun 18, 2005 10:09 am 
Regular
Regular

Joined: Mon May 16, 2005 2:15 pm
Posts: 59
tflorian wrote:
I started to think that transactions are optional and used only if you want to have the choice of rolling back. (that even if I don't use transactions changes will be written when I call session.Close())

But in this case it doesn't seem to be so.... was there a change between 0.7 and 0.8.4?



Transaction are optional. But, transaction.commit inherintly does a Session.Flush(). So, if you don't use transactions you have to Flush. There are some times when flush happens automatically though.

I don't think this has changed since .7.

BOb


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 18, 2005 11:45 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Calling Update doesn't do anything at all for persistent objects. Save/Update/Delete only record the needed changes in the session, and Flush executes them.

Inserts do happen during Save if you use the "identity" id generator, because NHibernate has to return the identifier of the new object and it's not possible to do without actually inserting the object.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 18, 2005 3:33 pm 
Newbie

Joined: Fri Jun 17, 2005 9:38 pm
Posts: 2
Location: Calgary, Canada
Ok that explains it ... thank you

Tomas


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