这是一个真正的 Hibernate 初学者问题。我对两个表的映射有疑问。第一个表是 MARKET,第二个表是 MARKET_MENU_BRANCH,其中包含每个 MARKET 行的行列表。当我保存新市场时,我希望它插入 MARKET 和 MARKET_MENU_BRANCH 行,但实际上它似乎插入 MARKET,然后尝试更新 MARKET_MENU_BRANCH 行,这些行不存在,从而导致错误。我做错了什么?我的表格如下所示:
mysql> describe MARKET;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(100) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
mysql> describe MARKET_MENU_BRANCH;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| marketId | int(11) | NO | PRI | 0 | |
| sequence | int(11) | NO | PRI | 0 | |
| name | varchar(100) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
我的域对象如下所示:
public class Market implements Serializable {
private int id;
private String name;
private List<MarketMenuBranch> marketMenuBranches;
// accessors / mutators etc...
public class MarketMenuBranch implements Serializable {
private MarketMenuBranchId id;
private String name;
// accessors / mutators etc...
public class MarketMenuBranchId implements Serializable {
private int marketId;
private int sequence;
// accessors / mutators etc...
我的映射如下:
<class name="Market" table="MARKET">
<id name="id"/>
<property name="name"/>
<list name="marketMenuBranches">
<key column="marketId"/>
<list-index column="sequence"/>
<one-to-many class="MarketMenuBranch"/>
</list>
</class>
和
<class name="MarketMenuBranch" table="MARKET_MENU_BRANCH">
<composite-id name="id" class="MarketMenuBranchId">
<key-property name="marketId"/>
<key-property name="sequence"/>
</composite-id>
<property name="name"/>
</class>
最佳答案
您有 parent / child 关系,您会在这个 page of the doc 中找到见解.
基本上,您需要保存(...)子级或使用级联属性。
关于java - 为什么此映射会导致尝试更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2715874/