title: Hibernate Foreign Key Constraint fails
body: <p>I want to create to SQL database tables named <strong>Customer</strong> and <strong>Orders</strong>
  SQL : </p>
  
  <pre><code>CREATE TABLE Customer (
    customer_id INT NOT NULL AUTO_INCREMENT,
    customer_name VARCHAR(255) UNIQUE,
    PRIMARY KEY (customer_id)
  );
  
  
  CREATE TABLE Orders (
    order_id INT NOT NULL AUTO_INCREMENT,
    customer_name VARCHAR(255),
    order_date DATETIME,
    PRIMARY KEY(order_id),
    FOREIGN KEY (customer_name) REFERENCES Customer(customer_name)
    ON UPDATE CASCADE
    ON DELETE CASCADE
  );
  </code></pre>
  
  <p>Afterwards I want to insert an Orders object with a reference to the Customer object.
  Java &amp; Hibernate:</p>
  
  <pre><code>Transaction tx = session.beginTransaction();
  Customer cu = (Customer) session.load(Customer.class, 1);
  
  Date d = new Date();
  Orders od = new Orders(cu, d);
  od.setCustomer(cu);
  
  Set&lt;Orders&gt; hs = new HashSet&lt;Orders&gt;();
  hs.add(od);
  cu.setOrderses(hs);
  
   session.save(cu);
   session.save(od);
   tx.commit();
  </code></pre>
  
  <p>The insert returns an exception : 
  <em>Cannot add or update a child row: a foreign key constraint fails</em>
  I am doing everything exactly as in the Hibernate Tutorials. 
  What am I doing wrong ?</p>
  
  <p>Java classes</p>
  
  <pre><code>    public class Customer  implements java.io.Serializable {
  
  
       private Integer customerId;
       private String customerName;
       private Set&lt;Orders&gt; orderses = new HashSet&lt;Orders&gt;(0);
  
      public Customer() {
      }
  
      public Customer(String customerName, Set&lt;Orders&gt; orderses) {
         this.customerName = customerName;
         this.orderses = orderses;
      }
  
      public Integer getCustomerId() {
          return this.customerId;
      }
  
      public void setCustomerId(Integer customerId) {
          this.customerId = customerId;
      }
      public String getCustomerName() {
          return this.customerName;
      }
  
      public void setCustomerName(String customerName) {
          this.customerName = customerName;
      }
      public Set&lt;Orders&gt; getOrderses() {
          return this.orderses;
      }
  
      public void setOrderses(Set&lt;Orders&gt; orderses) {
          this.orderses = orderses;
      }
  
  }
  
      public class Orders  implements java.io.Serializable {
  
  
       private Integer orderId;
       private Customer customer;
       private Date orderDate;
  
      public Orders() {
      }
  
      public Orders(Customer customer, Date orderDate) {
         this.customer = customer;
         this.orderDate = orderDate;
      }
  
      public Integer getOrderId() {
          return this.orderId;
      }
  
      public void setOrderId(Integer orderId) {
          this.orderId = orderId;
      }
      public Customer getCustomer() {
          return this.customer;
      }
  
      public void setCustomer(Customer customer) {
          this.customer = customer;
      }
      public Date getOrderDate() {
          return this.orderDate;
      }
  
      public void setOrderDate(Date orderDate) {
          this.orderDate = orderDate;
      }
  
  }
  </code></pre>