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();
}
|
|