-->
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.  [ 1 post ] 
Author Message
 Post subject: Updating Objects with a many-to-many mapping
PostPosted: Wed Jul 20, 2005 4:21 am 
Newbie

Joined: Wed Jul 20, 2005 3:42 am
Posts: 3
hi to all,

HELP

1. Example code on how to update a many-to-many mapping.
2. How to retrieve the HashSet() that is contained on an Object.


I am able to insert and delete object, even with many-to-many mapping, into the datebase however what I cannot figure out is how to retrieve them and then update them.

I looked at the Hibernate Reference Documentation for 3.0.5 Chapter 2, it only showed tutorials or instructions to session.save() a many to many mappings and I patterned my code from that. And the succeeding chapters discussed advanced features which I don't really need at the moment.

This is now the saving of the ContactDetails where PersonalInfo already Exists.

Code:
 
        PersonalInfo personalInfo = (PersonalInfo)session.load(PersonalInfo.class, this.pKey);
/*
   object - is passed from a GUI to a method;
*/
        ContactDetails contactDetails = (ContactDetails)object;
        session.save(contactDetails);
        personalInfo.getContactDetails().add(contactDetails);
        session.update(personalInfo);


All went well it even generated a personalinfo_contactdetails table and it contains both the ids of the two tables.

All I really need is a sample code on how to update the ConatactDetails...
I thought it would be as easy as session.update(cd, pKey)
where pKey is retrieve from PersonalInfo pi = session.load(PersonalInfo.class, new Integer(1) );

I am able to retrieve the PersonalInfo but the Set is null;

Also I need to know how to query the ParentInfo where the Set is also populated with its data.

Thanks so much...

Hibernate is really cool... I figured that hibernate is really difficult to learn because almost of the tutorial I read are just mapping, saving, updating and retrieving single class... or when there is and example of a many-to-many class, they don't show how to retrieve them and update them.
Plus the caveat emptor is way to advanced for me. Maybe just a basic AddressBook Application with separate classes for Addresses, Names, and TelephoneNumbers might do the job... if somebody can make a full featured tutorial for that and not using Advanced Design Patterns. Just basic, ineffiecent and not for production tutorial thanks :)




Hibernate version: 3.05

Mapping documents:

FOR PersonalInfo.class
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="model">
<class name="PersonalInfo" table="PERSONALINFO">
<id name="id" column="ID" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="firstName" column="FIRSTNAME" type="java.lang.String"/>
<property name="lastName" column="LASTNAME" type="java.lang.String"/>

<set name="contactDetails" table="PERSONALINFO_CONTACTDETAILS" inverse="true">
<key column="PERSONALINFOID"/>
<many-to-many column="CONTACTDETAILSID" class="ContactDetails"/>
</set>
</class>
</hibernate-mapping>

FOR ContactDetails.class

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="model">
<class name="ContactDetails" table="CONTACTDETAILS">
<id name="id" column="ID" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="emailAddress" column="EMAILADDRESS" type="java.lang.String"/>
<property name="phoneNumber" column="PHONENUMBER" type="java.lang.String"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Code:

Integer pKey = new Integer(12);

PersonalInfo pi = (PersonalInfo)session.load(PersonalInfo.class, this.pKey);

/*This should retrieve the first contactDetail that is inside the PesonalInfo class*/

ContactDetails cd = (ContactDetails)pi.getContactDetails().iterate().next();
cd.setPhoneNumber("NEW phone number");
session.update(cd);

//This should add the cd to a HashSet
pi.addContactDetails(cd);

session.update(pi);
tx.commit();



Name and version of the database you are using:
MySQL

The generated SQL (show_sql=true):
update PERSONALINFO set FIRSTNAME=?, LASTNAME=?, MIDDLENAME=?, NICKNAME=?, BIRTHDATE=? where ID=?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.