【问题标题】:Hibernate Foreign Key Constraint fails休眠外键约束失败
【发布时间】:2023-03-08 07:00:02
【问题描述】:

我想创建名为 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
);

之后,我想插入一个带有对 Customer 对象的引用的 Orders 对象。 Java 和休眠:

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

插入返回异常: 无法添加或更新子行:外键约束失败 我所做的一切都与 Hibernate 教程中的完全一样。 我做错了什么?

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

}

【问题讨论】:

  • 我认为您认为您不需要明确保存订单,当您保存客户时,hibernate会为您完成。
  • 我尝试了两种方式:使用级联和不使用级联:两者都返回相同的异常
  • 你能分享一下 CustomerOrders 类的代码吗?
  • 您使用 customer_name 作为外键而不是 customer_id 有什么原因吗?
  • customer_name 是包含相关名称的替代唯一键

标签: java mysql hibernate


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2016-08-17
    • 2011-11-03
    • 1970-01-01
    相关资源
    最近更新 更多