-->
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.  [ 3 posts ] 
Author Message
 Post subject: Saving a collection of List elements in one go
PostPosted: Wed May 17, 2006 12:43 pm 
Newbie

Joined: Wed May 17, 2006 12:24 pm
Posts: 3
Hi,

I seem to be a bit confused about using Hibernate with regards to 'dehydrating' a collection (list).

I think if I could get a really simple idea to work then it shouldn't be too tough to get a harder example to work.

Here's a pretty simple example with two objects and two tables.

house 1:* person (an house has zero, one or many people)

The tables are

house ( id, address )
person ( id, name, house_id )

(house_id being the FK of house.id)

The idea is I want to be able to add person objects to house with house.addPerson(person) and then at the end, save the house and all the people are saved too.

Now there are plenty examples for Set's but not List's. If I was to write this in SQL it would take only a few minutes so I'm not sure why Hibernate tries to make it so confusing.

Could someone provide a sample hibernate schema to save all the part's that belong to order when order is saved/updated) - I think the one I'm using is not right. The problem is some of the examples on the web have been really confusing brandishing id's around and its not clear which object table the id belongs to.

I've tried add() and merge() but both throw exceptions - I've googled them but I cannot find much help.

Any help you can provide (or even links to good examples) is greatly appreciated,

Thanks!

JCC


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 12:57 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Code:
<?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="learn.hibernate">
   <class name="House" table="house">
      <id name="id" column="id" />
      <property name="address" column="address"/>
      <set name="persons" lazy="true" inverse="true" cascade="all-delete-orphan">
         <key column="id"/>
         <one-to-many class="Person"/>
      </set>
    </class>
   
   <class name="Person" table="person">
      <id name="id" column="id" />
      <property name="name" column="name"/>
      <property name="houseId" column="house_id"/>
      <many-to-one name="houseObject" class="House" column="house_id" insert="false" update="false"/>
    </class>
</hibernate-mapping>



Code:
Session session = getSession( ... );
Transaction tx = session.beginTransaction();
tx.begin();
Person person1 = createPerson( ... );
Person person2 = createPerson( ... );

House house = createHouse( ... );
house.getPersons().add( person1 );
house.getPersons().add( person2 );

person1.setHouseObject( house );
person2.setHouseObject( house );

session.save( house );
tx.commit();


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Thu May 18, 2006 7:58 am 
Newbie

Joined: Wed May 17, 2006 12:24 pm
Posts: 3
That example was very helpful to me. I now have it working in a List (I first did it with a map just to see how it all fits together).

I didn't realize (and I'm still not really got my head around it) but the Person needs a house reference which doesn't make an awful lot of sense. If you save the house then house.getId() would be the reference for each FK in people object in house.getPeople() so you shouldn't really need a people.setHouse(...) but thats me nitpicking hibernate - you example has helped me understand hibernate very quickly.

Thanks,

JCC


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