我正在按照來自here的教程進行操作,並且有一部分我不明白。休眠多對多示例
在4.運行它 - 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();
股票和組別之間的關聯已經建立,而
stockCategory.setStock(stock); stockCategory.setCategory(category1);
那麼,爲什麼我們仍然需要
stock.getStockCategories().add(stockCategory);
案例謝謝!!
您可能還想檢查一下:http://stackoverflow.com/questions/824600/saving-bidirectional-manytomany –