java—在SpringMVC项目中通过jdbc语句执行ddl时出错

syqv5f0l  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(293)

我正试图运行我的JavaSpringMVC项目应用程序,但出现以下错误(我已将mysql数据库连接到该项目):

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement

其原因是:

Caused by: java.sql.SQLException: Cannot add foreign key constraint

上面的外键问题是关于我的数据库中的“orderitems”实体的,下面是它的代码:

package com.example.WebAppProcess20.Entities;

import javax.persistence.*;
import java.util.Objects;

@Entity
@Table(name = "ordersitems", schema = "theprocess")
@IdClass(OrdersitemsEntityPK.class)
public class OrdersitemsEntity {
    private String orderId;
    private String productId;
    private Integer qunatity;
    private OrdersEntity ordersByOrderId;

    @Id
    @Column(name = "orderId")
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    @Id
    @Column(name = "product_id")
    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    @Basic
    @Column(name = "qunatity")
    public Integer getQunatity() {
        return qunatity;
    }

    public void setQunatity(Integer qunatity) {
        this.qunatity = qunatity;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OrdersitemsEntity that = (OrdersitemsEntity) o;
        return Objects.equals(orderId, that.orderId) &&
                Objects.equals(productId, that.productId) &&
                Objects.equals(qunatity, that.qunatity);
    }

    @Override
    public int hashCode() {

        return Objects.hash(orderId, productId, qunatity);
    }

    @ManyToOne
    @JoinColumn(name = "orderId", referencedColumnName = "orderId", nullable = false, updatable = false, insertable = false)
    public OrdersEntity getOrdersByOrderId() {
        return ordersByOrderId;
    }

    public void setOrdersByOrderId(OrdersEntity ordersByOrderId) {
        this.ordersByOrderId = ordersByOrderId;
    }
}

此实体指定每个订单中包含的产品,因此它有两个外键-productid和orderid。
如果需要,还可以添加pom.xml和hibernate.cfg.xml文件:
hibernate.cfg.xml文件:

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:mysql://localhost:3306/theprocess?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC</property>
    <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- DB schema will be updated if needed -->
    <property name="hbm2ddl.auto">update</property>
    <mapping class="com.example.WebAppProcess20.Entities.ClientsEntity"/>
    <mapping class="com.example.WebAppProcess20.Entities.InvoicesEntity"/>
    <mapping class="com.example.WebAppProcess20.Entities.OrdersEntity"/>
    <mapping class="com.example.WebAppProcess20.Entities.OrdersitemsEntity"/>
    <mapping class="com.example.WebAppProcess20.Entities.ProductsEntity"/>
  </session-factory>
</hibernate-configuration>

pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>WebAppProcess2.0</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>WebAppProcess2.0</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

你能解释一下问题是什么吗?谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题