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!!