2010-04-26 76 views
2

這是一個真正的初學者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> 

回答

4

你有一個家長/孩子的關係,你會發現見解這page of the doc

基本上你需要保存(...)孩子或使用級聯屬性。

相關問題