我正在尝试向关系表 Stock Category 中插入一行。
我正在关注这个例子:http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/
现在我已经在表 stock 和 category 中有了数据。
稍后我想将股票和类别相互关联。
如何在不编写自定义 sql 查询的情况下执行此操作?
是否可以这样添加 StockCategory?
Stock stock = new Stock();
stock.setStockId(1);
Category category = new Category();
category.setCategoryId(1);
StockCategory stockCategory = new StockCategory();
stockCategory.setStock(stock); //here you need to get the stock object by id
stockCategory.setCategory(category1); //here you need to get the category1 object by id
stockCategory.setCreatedDate(new Date()); //extra column
stockCategory.setCreatedBy("system"); //extra column
session.save(stockCategory );
提前致谢。
最佳答案
StockCategory stockCategory = new StockCategory();
stockCategory.setStock(stock); //here you need to get the stock object by id
stockCategory.setCategory(category1); //here you need to get the category1 object by id
stockCategory.setCreatedDate(new Date()); //extra column
stockCategory.setCreatedBy("system"); //extra column
session.save(stock);
也有
关于java - 如何使用 hibernate 方法插入一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15478168/