简体   繁体   English

休眠外键约束失败

[英]Hibernate Foreign Key Constraint fails

I want to create to SQL database tables named Customer and Orders SQL : 我想创建名为CustomerOrders SQL的SQL数据库表:

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

Afterwards I want to insert an Orders object with a reference to the Customer object. 之后,我想插入一个带有对Customer对象的引用的Orders对象。 Java & Hibernate: Java和Hibernate:

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<Orders> hs = new HashSet<Orders>();
hs.add(od);
cu.setOrderses(hs);

 session.save(cu);
 session.save(od);
 tx.commit();

The insert returns an exception : Cannot add or update a child row: a foreign key constraint fails I am doing everything exactly as in the Hibernate Tutorials. 插入返回一个异常: 无法添加或更新子行:外键约束失败我所做的一切都与Hibernate Tutorials中的完全一样。 What am I doing wrong ? 我究竟做错了什么 ?

Java classes Java类

    public class Customer  implements java.io.Serializable {


     private Integer customerId;
     private String customerName;
     private Set<Orders> orderses = new HashSet<Orders>(0);

    public Customer() {
    }

    public Customer(String customerName, Set<Orders> 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<Orders> getOrderses() {
        return this.orderses;
    }

    public void setOrderses(Set<Orders> 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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM