-->
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.  [ 5 posts ] 
Author Message
 Post subject: Polite way to merge a detached object with unsync relations
PostPosted: Thu Nov 23, 2006 7:26 pm 
Newbie

Joined: Thu Jul 21, 2005 10:19 pm
Posts: 13
Hibernate version: 3.2.x + annotations

When populating an entity from the servletRequest parameters to be
updated in the database we have a detached entity in hands. but it has
probably no relations populated, although it may have on the database.

Simply merging it will lose our pre existant relationship.

For example, when editing a Book, maybe I will have a Book with a null
author @ManyToOne relation, although in the database there is the
relation with author id 1. i.e. My book object read from the web is filled by the controller as:

Code:
b = new Book();
b.setWhatever("something");
// do not invoke b.setAuthor as the author wasnt in the form


If i merge the above object, I will lose the current author in the database.

I dont want to write a custom persister for this, neither would like
to copy properties from a managed entity loading it from the session.

Would I have to use stateful logic beans (ejb or not), to keep track of the object from a previous request? How do you do this? The polite/elegant way....

_________________
http://www.caelum.com.br
xstream, paranamer and waffle .codehaus.org


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 8:23 pm 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
mmm, you can easily have the associated objects with only primary keys populated.

you can have just an Author with only the id populated, and assign it as the author of your Book, and that's all...

you can even use session.load with that primary keys to access proper proxies...

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 4:35 am 
Newbie

Joined: Thu Jul 21, 2005 10:19 pm
Posts: 13
pasha wrote:
mmm, you can easily have the associated objects with only primary keys populated.


Of course, but in order to have the primary keys in hand, I will have to first load the object at some point... and thats what Im trying to run away from...

What about:

1. load (will retrieve a proxy)
2. call proper setters, do not call some of them
3. invoke merge

Will it avoid the load? I will try it...

_________________
http://www.caelum.com.br
xstream, paranamer and waffle .codehaus.org


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 5:36 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
did you try?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 9:33 pm 
Newbie

Joined: Thu Jul 21, 2005 10:19 pm
Posts: 13
emmanuel wrote:
did you try?


I did some time ago, but had no time to post last week,

With @ManyToOne(fetch-lazy),
@org.hibernate.annotations.Entity(dynamicUpdate=true, selectBeforeUpdate=false):

1) it did a first lookup and it loaded the eager members + the foreign key

2) update simply updated the login field. PERFECT

Is there any step to remove the select statement on the annotated line (see code)?

Code:
      Session session = factory.getSession();
      Transaction tx = session.beginTransaction();
      Car car = new Car();
      car.setName("beetle");
      session.save(car);
      User user = new User();
      user.setName("name");
      user.setPassword("old");
      user.setLogin("old");
      user.setCar(car);
      session.save(user);
      tx.commit();
      
      System.out.println("User and car saved");

      session.clear();
      
      // tries to update leaving relation as it was
      tx = session.beginTransaction();
      User loaded = (User) session.load(User.class, user.getId());
                // QUESTION HERE
                // it would be great if setLogin would not select before update...
               // is it possible?
      loaded.setLogin("newlogin");
      session.update(loaded);
      tx.commit();
      
      System.out.println("User updated");

      session.clear();

      // tries to update leaving relation as it was
      tx = session.beginTransaction();
      User found = (User) session.load(User.class, user.getId());
      System.out.println("Found user " + found.getLogin());
      System.out.println("His car is " + found.getCar().getName());
      tx.commit();


generates

Code:
Hibernate: insert into Car (id, name) values (null, ?)
Hibernate: call identity()
Hibernate: insert into User (id, car_id, login, password, name) values (null, ?, ?, ?, ?)
Hibernate: call identity()
User and car saved
Hibernate: select user0_.id as id2_0_, user0_.car_id as car5_2_0_, user0_.login as login2_0_, user0_.password as password2_0_, user0_.name as name2_0_ from User user0_ where user0_.id=?
Hibernate: update User set login=? where id=?
User updated
Hibernate: select user0_.id as id2_0_, user0_.car_id as car5_2_0_, user0_.login as login2_0_, user0_.password as password2_0_, user0_.name as name2_0_ from User user0_ where user0_.id=?
Found user newlogin
Hibernate: select car0_.id as id0_0_, car0_.name as name0_0_ from Car car0_ where car0_.id=?
His car is beetle


So the question in the topic has been solved, now Im moving for a "selectBeforeUpdate" question in the same topic... should I open another one?

Thanks again...

_________________
http://www.caelum.com.br
xstream, paranamer and waffle .codehaus.org


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