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=?