Я использую весеннюю загрузку и спящий режим для простого веб-приложения.Когда я пытаюсь запустить его, я получаю эту ошибку 3 раза.
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applyForeignKeys(AbstractSchemaMigrator.java:433) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:249) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:183) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:312) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:460) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) [spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) [spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) [spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) [spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) [spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1761) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1698) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at com.reminder.EJA.EjaApplication.main(EjaApplication.java:10) ~[classes/:na]
Caused by: java.sql.SQLException: Cannot add foreign key constraint
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) ~[mysql-connector-java-5.1.6.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) ~[mysql-connector-java-5.1.6.jar:na]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) ~[mysql-connector-java-5.1.6.jar:na]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) ~[mysql-connector-java-5.1.6.jar:na]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) ~[mysql-connector-java-5.1.6.jar:na]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) ~[mysql-connector-java-5.1.6.jar:na]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536) ~[mysql-connector-java-5.1.6.jar:na]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465) ~[mysql-connector-java-5.1.6.jar:na]
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:734) ~[mysql-connector-java-5.1.6.jar:na]
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95) ~[HikariCP-2.7.8.jar:na]
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-2.7.8.jar:na]
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
... 33 common frames omitted
Он говорит: «Невозможно добавить ограничение внешнего ключа».Я проверял имена столбцов, также я искал здесь на stackoverflow, но почему-то я не могу найти ответ.
Это мой файл application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/EJA
spring.datasource.username=root
spring.datasource.password=database
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto = update
Это моя схема базы данных:
Вот также сценарий создания:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema EJA
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema EJA
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `EJA` DEFAULT CHARACTER SET utf8 ;
USE `EJA` ;
-- -----------------------------------------------------
-- Table `EJA`.`client`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `EJA`.`client` (
`idClient` INT NOT NULL AUTO_INCREMENT,
`nickname` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idClient`),
UNIQUE INDEX `Nickname_UNIQUE` (`nickname` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `EJA`.`priority`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `EJA`.`priority` (
`idPriority` INT NOT NULL AUTO_INCREMENT,
`priority` INT UNSIGNED NOT NULL,
PRIMARY KEY (`idPriority`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `EJA`.`state`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `EJA`.`state` (
`idState` INT NOT NULL AUTO_INCREMENT,
`state` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idState`),
UNIQUE INDEX `state_UNIQUE` (`state` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `EJA`.`event`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `EJA`.`event` (
`idEvent` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`from` DATE NOT NULL,
`to` DATE NOT NULL,
`description` VARCHAR(250) NULL,
`Client_idClient` INT NOT NULL,
`Priority_idPriority` INT NOT NULL,
`State_idState` INT NOT NULL,
PRIMARY KEY (`idEvent`, `Priority_idPriority`, `State_idState`, `Client_idClient`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC),
INDEX `fk_Event_Client_idx` (`Client_idClient` ASC),
INDEX `fk_Event_Priority1_idx` (`Priority_idPriority` ASC),
INDEX `fk_Event_State1_idx` (`State_idState` ASC),
UNIQUE INDEX `description_UNIQUE` (`description` ASC),
CONSTRAINT `fk_Event_Client`
FOREIGN KEY (`Client_idClient`)
REFERENCES `EJA`.`client` (`idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Event_Priority1`
FOREIGN KEY (`Priority_idPriority`)
REFERENCES `EJA`.`priority` (`idPriority`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Event_State1`
FOREIGN KEY (`State_idState`)
REFERENCES `EJA`.`state` (`idState`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
А это мои классы сущностей (без методов установки / получения и импорта):
@Entity
@Table(name = "event")
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idEvent")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "from")
private Date from;
@Column(name = "to")
private Date to;
@Column(name = "description")
private String description;
@ManyToOne
@JoinColumn(name = "Client_idClient")
private Client client;
@ManyToOne
@JoinColumn(name = "State_idState")
private State state;
@ManyToOne
@JoinColumn(name = "Priority_idPriority")
private Priority priority;
}
@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue
@Column(name = "idClient")
private Integer id;
@Column(name = "nickname")
private String nickname;
@Column(name = "password")
private String password;
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
private Collection<Event> events;
}
@Entity
@Table(name = "priority")
public class Priority {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idPriority")
private Integer id;
@Range(min = 0, max = 10)
@Column(name = "priority")
private Integer priority;
@OneToMany(mappedBy = "priority")
private Collection<Event> event;
}
@Entity
@Table(name = "state")
public class State {
@Id
@Column(name = "idState")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Pattern(regexp = "")
@Column(name = "state")
private String state;
@OneToMany(mappedBy = "state")
private Collection<Event> event;
}
Спасибо за любую помощь.