This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Saving Associated objects

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have one very basic question to ask.

I have 2 tables

Book [Parent table]
Chapter [Child table]

There is a one-to-many relationship between these 2 tables. 1 book has many chapters. Book objects holds collection of Chapter objects.

I am using Spring and Hibernate in my application. We designed our DAO layer to keep the persistence logic separate from application.

Requirement is that, user will send the books and chapters information 2 one method. This method is in Spring manager class. Now we have to store information in both of these tables, as these objects are associated.

My confusion is how to store these 2 objects in 2 different tables with a single save.

As I am getting information about these 2 objects in Spring layer I can't open Hibernate session there as well as not clear either to pass populated objects to DAO layer, which will open the session and will store these objects one by one.

I know this is a basic problem, please guide me.


Thanks
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to either save the objects "manually" in the same transaction:



or specify an appropriate cascade level on the relationship (see hibernate docu)

pascal
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are variuos ways to accomplish it. One approach is -

Book book = new Book();
book.setBookid(100);
book.setBookname("Hibernate in Action");

Chapter chapterOne = new Chapter();
chapterOne.setChapterid(1001);
chapterOne.setChaptername("Introduction to Hibernate");

Chapter chapterTwo = new Chapter();
chapterOne.setChapterid(1002);
chapterOne.setChaptername("Understanding Persistence");


Set chapterSet = new HashSet();
chapterSet.add(chapterOne);
chapterSet.add(chapterTwo);

book.setChapters(chapterSet);

session.persist(book);
tx.commit();
In chapter.hbm.xml you have define <many-to-one> and book.hbm.xml you have define
<set name="chapters" .....>
..
..
<one-to-many class="Chapter" ../>
</Set>

Similary you have to write POJO.

I hope this will help.
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic