-->
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: Trouble persisting a set
PostPosted: Thu Sep 04, 2003 2:42 pm 
Newbie

Joined: Thu Sep 04, 2003 2:30 pm
Posts: 4
I sifted through the documentation and this forum, but am having trouble finding what I am doing wrong. I am persisting a class "Person" which contains a set of "Address" objects. The person persists fine, but none of the addresses in the set persist. Here is what I am doing ...

person.hbml.xml
<hibernate-mapping>
<class name="com.uvalodge.person.Person" table="person">

<id name="uid">
<generator class="identity"/>
</id>

<property name="firstName"/>
<property name="lastName"/>
<set name="addresses" lazy="true">
<key column="person_uid"/>
<one-to-many class="com.uvalodge.person.Address"/>
</set>
</class>
</hibernate-mapping>

address.hbm.xml
<hibernate-mapping>
<class name="com.uvalodge.person.Address" table="address">
<id name="uid">
<generator class="identity"/>
</id>
<property name="address1"/>
<property name="address2"/>
<property name="city"/>
<property name="state"/>
<property name="zip"/>
</class>
</hibernate-mapping>

Then in my calling code I do the following....

Configuration cfg = new Configuration();
cfg.addClass(Person.class).addClass(Address.class);

Person p = new Person();
p.setFirstName("First");
p.setLastName("Last");

Address a = new Address();
a.setAddress1("Here");
a.setAddress2("Apt 101");
a.setCity("Arlington");
a.setState("VA");
a.setZip("22203");

Set s = new HashSet();
s.add(a);
p.setAddresses(s);

SessionFactory sessionFactory = cfg.buildSessionFactory();
Session sess = sessionFactory.openSession();
Transaction transaction = sess.beginTransaction();
sess.save(p);
transaction.commit();
sess.close();

I tried setting the attribute cascade="all" on the set element, but it does not seem to affect anything. Anyone have any thoughts?


Top
 Profile  
 
 Post subject: Missing save(a);
PostPosted: Thu Sep 04, 2003 3:37 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
You have this:
Person p = new Person();
p.setFirstName("First");
p.setLastName("Last");

Address a = new Address();
a.setAddress1("Here");
a.setAddress2("Apt 101");
a.setCity("Arlington");
a.setState("VA");
a.setZip("22203");

Set s = new HashSet();
s.add(a);
p.setAddresses(s);

SessionFactory sessionFactory = cfg.buildSessionFactory();
Session sess = sessionFactory.openSession();
Transaction transaction = sess.beginTransaction();
sess.save(p);
transaction.commit();
sess.close();

You forgot
session.save(a);
you should have
================
.....
session.save(a);
sess.save(p);
transaction.commit();
sess.close();


Top
 Profile  
 
 Post subject: OK, that works
PostPosted: Thu Sep 04, 2003 3:56 pm 
Newbie

Joined: Thu Sep 04, 2003 2:30 pm
Posts: 4
Thanks for the response, and when I place the session.save(a), everything works as expected. I would have thought that when you persist a class that contains a Set, it would also persist the individual items within that Set. But you are saying that I will need to iterate over the Set and individually persist each object via session.save(). Is there no way for Hibernate to save the individual items in a set when it is contained within another object that is being persisted?

Thanks for your help--I really appreciate you helping out a newbie :)


Top
 Profile  
 
 Post subject: not sure
PostPosted: Thu Sep 04, 2003 5:03 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
I'm no hibernate expert either. You can look at the docs with the cascade option though. cascade="all" might do what you wish.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 10:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Please read the Hibernate documentation. Pay particular attention to the Parent/Child Relationship section.


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.