Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8525037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:51:31+00:00 2026-06-11T07:51:31+00:00

I am following the tutorial from here , and there is one part that

  • 0

I am following the tutorial from here, and there is one part that I do not understand.

At 4. Run it – Case 1

    session.beginTransaction();
    Stock stock = new Stock();
    stock.setStockCode("7052");
    stock.setStockName("PADINI");
    Category category1 = new Category("CONSUMER", "CONSUMER COMPANY");
    session.save(category1);

    StockCategory stockCategory = new StockCategory();
    stockCategory.setStock(stock);
    stockCategory.setCategory(category1);
    stockCategory.setCreatedDate(new Date()); //extra column
    stockCategory.setCreatedBy("system"); //extra column

    stock.getStockCategories().add(stockCategory);

    session.save(stock);

    session.getTransaction().commit();

The association between stock and category1 has been created while

    stockCategory.setStock(stock);
    stockCategory.setCategory(category1);

Then, why do we still need

stock.getStockCategories().add(stockCategory);

Thanks!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:51:33+00:00Added an answer on June 11, 2026 at 7:51 am

    Strictly speaking the add is not necessary for hibernate to persist the objects. If you were to explicitly save the stock category then the relationship would be persisted. However you’re only saving the stock object. Because of this the stock category needs to be in the collection so that hibernate can find it.

    Notice the CascadeType.ALL on the categories collection. This means that on save, hibernate should look at the items in this collection and save them all. Because of this saving the stock is sufficient for hibernate to find and save the stock category and persist the relationship. If you did not add the item to the collection hibernate would not be able to find it so the relationship would not be saved unless you explicitly saved the StockCategory.

    As mentioned in other answers, it’s also generally considered good practice to keep the object graph consistent with the persistent state to avoid subtle bugs where what is populated is different at different times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following the tutorial here: http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-part-1 But I'm having trouble with Part 4:
I'm following the kernel tutorial from here im having problems compiling my files. i
I have been following the affableBean tutorial from the NetBeans site located here .
Is there a tutorial on how to run Ant from Java? I got some
I am following a tutorial from nettuts, specifically here: http://blog.themeforest.net/tutorials/jquery-for-absolute-beginners-day-11/ I am 3/4's of
In the following tutorial: http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/7_3.html There is the part: test_parser :- repeat, write('?? '),
From the following paragraphs of Text—— (http://developer.yahoo.com/hadoop/tutorial/module2.html),It mentions that sequential readable large files are
Following a tutorial from iTunes U on how to do face detection (the tutorial
I managed to get authentication to work by following the tutorial from this page
I am learning GWT, so I am following this tutorial from Google GWT https://developers.google.com/web-toolkit/tools/gwtdesigner/tutorials/stockwatcher

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.