Why does this display lead to update attempts?

This is a real hibernation problem. I have a problem with mappings for two tables. The first table is MARKET, the second is MARKET_MENU_BRANCH, which contains a list of lines for each MARKET line. When I save a new market, I want it to insert the MARKET and MARKET_MENU_BRANCH rows, but it actually seems to insert the MARKET and then try to update the MARKET_MENU_BRANCH rows that don't exist, which causes an error. What am I doing wrong? My tables look like this:

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    |       | 
+----------+--------------+------+-----+---------+-------+

      

My domain objects look like this:

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...

      

and my mappings look like this:

<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>

      

and

<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>

      

+2


a source to share


1 answer


You have a parent / child relationship, you will find information on this page of the doc .



Basically you need to keep (...) the child or use the cascade attribute.

+4


a source







All Articles