-->
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: beginner question update table
PostPosted: Mon Feb 01, 2010 12:11 pm 
Newbie

Joined: Fri Nov 20, 2009 9:26 am
Posts: 4
I'm a beginner and I don't figure how to update 2 simple tables:
I have one table Person and the other Phone.The Person table has a name, a surname etc. The Phone table has a number and a description.
Between the 2 tables is a one to many relation. A person has one or more phone numbers.

I don't know how I must do when I have to update a person information , adding it another phone number.My ideea was that I have to find out what phones corespod to that persons , put it in a set and in the end add to that set the new phone number and update the both tables.
I need to know what is the normal way of doing that cause I'm sure I'm not doing the things right.



java.util.Collection<Phone> phoneSet=new HashSet<Phone>();
session=sessionFactory.openSession();
Phone ph=new Phone();

for(Phone phone: person.getPhones()){
ph.setId(phone.getId());
ph.setName(phone.getName());
ph.setSurname(phone.getSurname());
phoneSet.add(ph);
ph=new Phone();
}

session.close()

phoneSet.add(newPhone);


Top
 Profile  
 
 Post subject: Re: beginner question update table
PostPosted: Mon Feb 01, 2010 8:19 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Create the Person.
Create the Phone.
Add the phone to the person using a setter.

Save the phone.
Save the person.

The phone and the person will be saved, along with the relationship between the two of them.

Go for it!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: beginner question update table
PostPosted: Mon Feb 01, 2010 9:57 pm 
Newbie

Joined: Mon Feb 01, 2010 12:19 am
Posts: 4
As for me, usually, problem of using hibernate happens in hbm.xml file. so if you can paste it, may be solved more concretely,


if you want to save person, and implicitly save the phone to that table, maybe you would append "cascade" with its value in terms of your need actual


Top
 Profile  
 
 Post subject: Re: beginner question update table
PostPosted: Wed Feb 03, 2010 9:27 am 
Newbie

Joined: Fri Nov 20, 2009 9:26 am
Posts: 4
Cameron McKenzie wrote:
Create the Person.
Create the Phone.
Add the phone to the person using a setter.

Save the phone.
Save the person.

The phone and the person will be saved, along with the relationship between the two of them.

Go for it!




Yes but the problem is that if I would do like that , I would lose the other phones that I have already entered previously and that are coresponding to the person I choose to update . I lose the foreign keys to be more exactly.


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.