-->
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.  [ 2 posts ] 
Author Message
 Post subject: one-to-many saving
PostPosted: Mon Jul 11, 2005 6:13 am 
Newbie

Joined: Mon Jul 11, 2005 4:35 am
Posts: 7
Hi

I want to save an Order to the database. An Order contains a set of OrderItems.
In the database there is an Order-table and an OrderItem-table, with
a one-to-many relationship between them.

When I'm to save my Order I've already been setting the OrderItems. My
question is if there is a way to just call session.save(order) and even the
OrderItems get persisted? Or is it necessary to call session.save(items[i])
even for each OrderItems like I've have done?

public void saveOrder(Order order) {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
// save order
session.save(order);
// save orderItems
Set items = order.getOrderItems();
OrderItem[] items= (OrderItem[]) set.toArray(new OrderItem[0]);
for (int i=0; i<items.length; i++) {
session.save(items[i]);
}
tx.commit();
HibernateUtil.closeSession();
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 6:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
read the docs especially about the cascade attribute

_________________
Max
Don't forget to rate


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