-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to update an object based on an attribute
PostPosted: Wed Mar 31, 2004 9:50 am 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
I have the following class:

public class Parent
{
protected long id;
protected String name;
protected List childs1;
protected List childs2;

...
}

I save an object with a specific name:

parent1 = new Parent ();
parent1.setName ("X");
parent1.setChilds1 (...);
parent1.setChilds2 (...);
session.save (parent1);

If I get another object with the same name I want to update the last object with all it's relationships.

parent2 = new Parent ();
parent2.setName ("X");
parent2.setChilds1 (...);
parent2.setChilds2 (...);
session.save (parent2);

How can I do this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 10:16 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If I understand well, you'll have to request the DB to retrieve all objects having the same name and do the job you want.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 10:17 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
mean find ("from MyObject obj where obj.name= :name", ...);

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Case
PostPosted: Wed Mar 31, 2004 2:20 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi, Thanks for the quick reply emmanuel

The thing is that my data access objects will receive a new instatiated object, not a session loaded object and I have to update the database with this object. I know that there is a saveOrUpdateCopy method but I'm not sure if I can use this for what I need.


Top
 Profile  
 
 Post subject: Re: Case
PostPosted: Wed Mar 31, 2004 2:44 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
nestorjb wrote:
Hi, Thanks for the quick reply emmanuel

The thing is that my data access objects will receive a new instatiated object, not a session loaded object and I have to update the database with this object. I know that there is a saveOrUpdateCopy method but I'm not sure if I can use this for what I need.


I do the same thing with my DAO layer. I get the initial object and pass it off to struts to display to forms. Once the user updates the object on screen, the changes are put together to construct a new instance of the object. I pass the new instance into the DAO and use saveOrUpdate() to do an update based on the primary key value. This is controlled by the following attribute in the mapping.

Code:
<id
            name="userGroupId"
            column="USERGROUPID"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="sequence">
                <param name="userGroupId">SEQ_USERGROUPID</param>
            </generator>
        </id>


Note, if the primaryKey is 0 instead of null, it will try to do an insert instead of an update.

Hope that helps.


Top
 Profile  
 
 Post subject: Problem with child objects
PostPosted: Wed Mar 31, 2004 11:17 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi gpani thanks for the reply

That works for the parent object but if you have child objects and you have cascade="all-delete-orphan" it won


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