• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate saving multiple hierarchy records

 
arul meh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have mapping like -

User has many Order
Order has many OrderItem
OrderItem has one Address.

When a user make order - I have to save User, Order, OrderItem, Address in database.

In my DAO, I am saving record for Order as

getSession().save(OrderData);

I have to further save Orderitem and Addresses. Please guide me how do I do save records in OrderItem ? First, should I get id from Order which is just created and then save that in OrderItems. Then get Id for OderItem when it is saved in database and then save Address, etc for that orderitem. Please help.

Thanks.
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate can handle that for you as long as the relationships are defined correctly in the mapping file or if you are using annotations in each entity class.

For example for the relationship between order and order item, using annotations you just have to define the relationship and identify the 1-many relationship using the @JoinColumn and @ManyToOne Annotations. I'd suggest you read up on Hibernate a little.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic