title: Hibernate parent/child relationship. Why is object saved twice?
body: <p>I am looking into Hibernate's parent/child relationships.  </p>
  
  <p>I have 3 entities <code>Employee</code> <code>Customers</code> and <code>Orders</code>.  </p>
  
  <p>Their relationship is<br>
   <code>Employee</code> 1 &lt;-> N <code>Orders</code><br>
   <code>Customer</code> 1 &lt;-> N <code>Orders</code>  </p>
  
  <p>I want to be able to save/update/delete <code>Customer</code> objects and <code>Employees</code> and <code>Orders</code> but I want to use some interface so that the calling code does not deal with any Hibernate or JPA stuff.  </p>
  
  <p>E.g. I tried something like the following:  </p>
  
  <pre><code>class Utils{
    public static void saveObject(Object o){      
        logger.debug(o.toString());
        Session session = getSessionFactory().openSession();      
        Transaction tx = session.beginTransaction();
        session.save(o);
        tx.commit();
         session.close();
     }
  }
  </code></pre>
  
  <p>and in the calling code I do:  </p>
  
  <pre><code>  Employee employee = new Employee();
   //set data on employee
   Customer customer = new Customer();
    //set data on customer
   Order order = new Order();
   //set data on order
   employee.addOrder(order);//this adds order to its list, order gets a reference of employee as parent
   customer.addOrder(order);//this adds order to its list, order gets a reference of customer as parent 
    Utils.saveObject(customer);
    Utils.saveObject(employee);
  </code></pre>
  
  <p>Now I noticed that with this code, 2 records of employee are created instead of 1.  </p>
  
  <p>If I only do:  </p>
  
  <p>Utils.saveObject(customer);</p>
  
  <p>Only 1 (correctly) record is created.  </p>
  
  <p>Why does this happen?<br>
  Does this happens because the same <code>Order</code> object is saved by both <code>Customer</code> and <code>Employee</code>? And the <code>cascade="all"</code> makes this side-effect?  </p>
  
  <p>Now if I do not use the <code>DBUtils</code> method and do directly:  </p>
  
  <pre><code> Session session = DBUtil.getSessionFactory().openSession();        
   Transaction tx = session.beginTransaction();
   session.save(employee);
   session.save(customer);
   tx.commit();
   session.close();
  </code></pre>
  
  <p>Again, it works as expected. I.e. only 1 employee record is created.  </p>
  
  <p>What am I doing something wrong here?   </p>
  
  <hr>
  
  <p><strong>UPDATE:</strong>  </p>
  
  <p>Hibernate mappings:  </p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="database.entities.Associate" table="EMPLOYEE"&gt;
          &lt;id name="assosiateId" type="java.lang.Long"&gt;
              &lt;column name="EMPLOYEEID" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;property name="firstName" type="java.lang.String" not-null="true"&gt;
              &lt;column name="FIRSTNAME" /&gt;
          &lt;/property&gt;
          &lt;property name="lastName" type="java.lang.String" not-null="true"&gt;
              &lt;column name="LASTNAME" /&gt;
          &lt;/property&gt;
          &lt;property name="userName" type="java.lang.String" not-null="true"&gt;
              &lt;column name="USERNAME" /&gt;
          &lt;/property&gt;
          &lt;property name="password" type="java.lang.String" not-null="true"&gt;
              &lt;column name="PASSWORD" /&gt;
          &lt;/property&gt;
          &lt;set name="orders" table="ORDERS" inverse="true" cascade="all" lazy="true"&gt;
              &lt;key&gt;
                  &lt;column name="EMPLOYEEID" /&gt;
              &lt;/key&gt;
              &lt;one-to-many class="database.entities.Order" /&gt;
          &lt;/set&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  
  
  
  &lt;hibernate-mapping&gt;
      &lt;class name="database.entities.Customer" table="CUSTOMER"&gt;
          &lt;id name="customerId" type="java.lang.Long"&gt;
              &lt;column name="CUSTOMERID" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;property name="customerName" type="java.lang.String"&gt;
              &lt;column name="CUSTOMERNAME" /&gt;
          &lt;/property&gt;
          &lt;set name="orders" table="ORDERS" inverse="true" cascade="all" lazy="true"&gt;
              &lt;key&gt;
                  &lt;column name="CUSTOMERID" /&gt;
              &lt;/key&gt;
              &lt;one-to-many class="database.entities.Order" /&gt;
          &lt;/set&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  
  
  &lt;hibernate-mapping&gt;
      &lt;class name="database.entities.Order" table="ORDERS"&gt;
          &lt;id name="orderId" type="java.lang.Long"&gt;
              &lt;column name="ORDERID" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;property name="orderDate" type="java.util.Date"&gt;
              &lt;column name="ORDERDATE" /&gt;
          &lt;/property&gt;        
          &lt;property name="quantity" type="java.lang.Integer"&gt;
              &lt;column name="QUANTITY" /&gt;
          &lt;/property&gt;
          &lt;property name="quantityMargin" type="java.lang.Long"&gt;
              &lt;column name="QUANTITYMARGIN" /&gt;
          &lt;/property&gt;
          &lt;property name="port" type="java.lang.String"&gt;
              &lt;column name="PORT" /&gt;
          &lt;/property&gt;
          &lt;property name="orderState" type="java.lang.String"&gt;
              &lt;column name="ORDERSTATE" /&gt;
          &lt;/property&gt;
          &lt;many-to-one name="customer" class="database.entities.Customer" cascade="all" fetch="join"&gt;
              &lt;column name="CUSTOMERID" /&gt;
          &lt;/many-to-one&gt;
          &lt;many-to-one name="associate" column="EMPLOYEEID" class="database.entities.Employee" cascade="all" fetch="join"&gt;
  
          &lt;/many-to-one&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>UDATE 2:</strong>  </p>
  
  <pre><code>class Employee{  
  //Various members  
    Set&lt;Order&gt; orders = new HashSet&lt;Order&gt;();  
    public void addOrder(Order order){  
       order.setEmployee(this);  
       orders.add(order);     
    }  
  }  
  </code></pre>
  
  <p>Also:</p>
  
  <pre><code>class Customer{  
  //Various members  
    Set&lt;Order&gt; orders = new HashSet&lt;Order&gt;();  
    public void addOrder(Order order){  
       order.setCustomer(this);  
       orders.add(order);     
    }  
  }  
  </code></pre>