为什么此映射会导致尝试更新?

时间:2010-04-26 18:26:33

标签: java hibernate orm

这是一个真正的初学者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>

1 个答案:

答案 0 :(得分:4)

您有父/子关系,您可以在此page of the doc找到见解。

基本上你需要保存(...)孩子或使用cascade属性。