在Spring-MVC项目中通过JDBC语句执行DDL错误

时间:2018-07-25 11:24:51

标签: java mysql hibernate spring-mvc

我正在尝试运行Java Spring MVC项目应用程序,但是出现以下错误(我已将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>

能否请您说明问题所在? 谢谢!

0 个答案:

没有答案