【发布时间】:2013-03-18 13:35:05
【问题描述】:
我正在尝试在关系表 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 );
提前致谢。
【问题讨论】:
-
教程中没有描述吗?
-
等待。我正在检查代码
-
在哪里有一些像 nullable = false 这样的字段。您需要为这些字段设置值
标签: java hibernate hibernate-mapping hibernate-annotations