-->
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.  [ 4 posts ] 
Author Message
 Post subject: Association manipulation question
PostPosted: Sun Jun 05, 2005 5:44 am 
Hi!

Why does one have to load objects first (via session) in order to manipulate them, if they are already loaded in memory (but the session was closed beforehand)?

Example:
//code omitted for brevity
Person p1 = new Person();
p1.Name = "Person One";
s.Save(p1);

Person p2 = new Person();
p2.Name = "Person Two";
s.Save(p2);
s.Close();

//this does not work!! (unless everything happens in the same session)
p1.Friends.Add(p2);

//this works
tx = ses.BeginTransaction();
p1 = s.Load(typeof(Person), 1);
p2 = s.Load(typeof(Person), 2);
p1.Friends.Add(p2);
tx.Commit();
s.Close();

So, the question, from the different angle is:

Why does a function like this does not work?
AddFriend(Person p, Person f) {
s = factory.Opensession();
tx = session.BeginTran();
p.Friends.Add(friend);
tx.Commit();
s.Close();

}

but this one works?
AddFriend(Person p, Person friendf) {
s = factory.Opensession();
tx = session.BeginTran();
s.load(typeof(Person)
p.Friends.Add(f);
tx.Commit();
s.Close();

}

What would be logical is maybe a call to s.SaveOrUpdate(p), since p's property (collection/Set/List) changed...

Thanks for any clarifications on the subject.

Miha


Top
  
 
 Post subject:
PostPosted: Sun Jun 05, 2005 7:47 am 
Senior
Senior

Joined: Sat May 14, 2005 8:40 am
Posts: 130
If your Friends collection is lazy-loaded, the Person who has the collection needs to be associated with a session, otherwise lazy-loading doesn't work.

If the Person is disconnected and you want to add some friends, you have to associate the Person with a session with ISession.Lock(person, LockMode.None).

_________________
Cuyahoga


Top
 Profile  
 
 Post subject: lazy-loading?
PostPosted: Sun Jun 05, 2005 9:32 am 
martijnb,

I have not specified lazy-loading (it defaults to false, right?). I presumed that if I have an object, and if I want to change this documents properties, that it is not necessary that it is loaded first (since the object has an ID anyway).

So, manipulating an object and saving it (without loading it first) would seem to work, but it does not.

Well, I guess that's just how it is, right? :)

Thanks,
Miha.


Top
  
 
 Post subject:
PostPosted: Sun Jun 05, 2005 3:46 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Your first example will work if you add a s.Update(p) before the call to commit. Otherwise the second session doesn't know about your object at all, so it won't persist any changes.


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