Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 由于外键损坏,Hibernate无法重建表_Java_Mysql_Spring_Hibernate_Jpa - Fatal编程技术网

Java 由于外键损坏,Hibernate无法重建表

Java 由于外键损坏,Hibernate无法重建表,java,mysql,spring,hibernate,jpa,Java,Mysql,Spring,Hibernate,Jpa,好的,我有一个使用Hibernate(5.0.12.Final)的Spring启动应用程序(1.5.2.RELEASE),我遇到了一些问题,Hibernate无法在启动时正确地重新创建数据库 我将Hibernate配置为在启动时再次创建表: spring.jpa.hibernate.ddl-auto=create 这是我的第一个实体(客户): 以下是所创建数据库的概述: 这是控制台输出,为了简单起见,我尝试将其缩写为: INFO : HCANN000001: Hibernate Common

好的,我有一个使用Hibernate(5.0.12.Final)的Spring启动应用程序(1.5.2.RELEASE),我遇到了一些问题,Hibernate无法在启动时正确地重新创建数据库

我将Hibernate配置为在启动时再次创建表:

spring.jpa.hibernate.ddl-auto=create
这是我的第一个实体(客户):

以下是所创建数据库的概述:

这是控制台输出,为了简单起见,我尝试将其缩写为:

INFO : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
INFO : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
INFO : HHH000227: Running hbm2ddl schema export

Hibernate: alter table reservation drop foreign key FKoewar6f18rkn4iptr6da4oysv

ERROR : HHH000389: Unsuccessful: alter table reservation drop foreign key FKoewar6f18rkn4iptr6da4oysv
ERROR : Can't DROP 'FKoewar6f18rkn4iptr6da4oysv'; check that column/key exists

Hibernate: drop table if exists client

ERROR : HHH000389: Unsuccessful: drop table if exists client
ERROR : Cannot delete or update a parent row: a foreign key constraint fails

Hibernate: drop table if exists reservation
Hibernate: drop table if exists restaurant
Hibernate: create table client (id bigint not null auto_increment, created_on datetime, credentials_version integer not null, hash varchar(255), username varchar(255), email varchar(255) not null, first_name varchar(255) not null, name varchar(255) not null, primary key (id))

ERROR : HHH000389: Unsuccessful: create table client (id bigint not null auto_increment, created_on datetime, credentials_version integer not null, hash varchar(255), username varchar(255), email varchar(255) not null, first_name varchar(255) not null, name varchar(255) not null, primary key (id))
ERROR : Table 'client' already exists

Hibernate: create table reservation (id bigint not null auto_increment, state varchar(255), client_id bigint, primary key (id))
Hibernate: create table restaurant (id bigint not null auto_increment, primary key (id))
Hibernate: alter table reservation add constraint FKoewar6f18rkn4iptr6da4oysv foreign key (client_id) references client (id)

 INFO  : HHH000230: Schema export complete
 INFO  : Initialized JPA EntityManagerFactory for persistence unit 'default'
 INFO  : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
 INFO  : HHH000227: Running hbm2ddl schema export
ERROR  : HHH000389: Unsuccessful: alter table `Reservation` drop foreign key `FK8gjvlt7rp8i9kp7rdmn107ni7`
ERROR  : Can't DROP 'FK8gjvlt7rp8i9kp7rdmn107ni7'; check that column/key exists
ERROR  : HHH000389: Unsuccessful: drop table if exists `Client`
ERROR  : Cannot delete or update a parent row: a foreign key constraint fails
ERROR  : HHH000389: Unsuccessful: create table `Client` (`id` bigint not null auto_increment, `createdOn` datetime, `credentialsVersion` integer not null, `hash` varchar(255), `username` varchar(255), `email` varchar(255) not null, `firstName` varchar(255) not null, `name` varchar(255) not null, primary key (`id`))
ERROR  : Table 'client' already exists
 INFO  : HHH000230: Schema export complete
我不知道这是否重要,但MySQL数据库版本是在Windows上运行的5.7.13。此外,我没有包括餐厅实体和凭证

我觉得很奇怪,Hibernate试图先删除一个不存在的外键,然后删除正确的外键,但都失败了(如果我读对了的话)

这背后的原因是什么?如何预防这些问题

我看过其他相关的SO问题,它们通常是由无效的名称或类似的名称引起的,但我没有发现我的命名有任何问题。我也尝试过在我所有的表前面加上“tbl_”前缀,并用前缀重命名一些列,以防发生冲突,但运气不好


这让我很烦恼,因为数据库没有正确更新,在活动开发期间,我不想每次都手动删除/创建模式。也许值得一提的是,即使模式为空,在启动时,hibernate也会给出一些不存在的错误。

试试看。我认为这是一个保留问题

@ManyToOne
@JoinColumn(name = "client_id")
private Client client;

首先,您应该了解外键约束名称是如何生成的:

因此,如果更改表或外键列名,则会有不同的外键约束名称,并且Hibernate将无法删除旧的外键约束

您可以做什么:

  • 使用指定外部约束名称
    @javax.persistence.ForeignKey
    注释(不要将其与
    @org.hibernate.annotations.ForeignKey
    ,尽管可以使用 至)

  • 使用
    spring.jpa.hibernate.ddl-auto=update
    只更新模式, 不是每次都要重现它

  • 使用命名策略生成外键名(仅适用于Hibernate 5)。但您需要考虑如何组合表、关联表、列名称,以避免经常更改外部约束名称:


  • 您好,我相信client_id已经是默认的名称了。谢谢您的回答,但是即使我在几代人和初创公司之间没有更改任何内容,它仍然无法删除外键。
    INFO : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
    INFO : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
    INFO : HHH000227: Running hbm2ddl schema export
    
    Hibernate: alter table reservation drop foreign key FKoewar6f18rkn4iptr6da4oysv
    
    ERROR : HHH000389: Unsuccessful: alter table reservation drop foreign key FKoewar6f18rkn4iptr6da4oysv
    ERROR : Can't DROP 'FKoewar6f18rkn4iptr6da4oysv'; check that column/key exists
    
    Hibernate: drop table if exists client
    
    ERROR : HHH000389: Unsuccessful: drop table if exists client
    ERROR : Cannot delete or update a parent row: a foreign key constraint fails
    
    Hibernate: drop table if exists reservation
    Hibernate: drop table if exists restaurant
    Hibernate: create table client (id bigint not null auto_increment, created_on datetime, credentials_version integer not null, hash varchar(255), username varchar(255), email varchar(255) not null, first_name varchar(255) not null, name varchar(255) not null, primary key (id))
    
    ERROR : HHH000389: Unsuccessful: create table client (id bigint not null auto_increment, created_on datetime, credentials_version integer not null, hash varchar(255), username varchar(255), email varchar(255) not null, first_name varchar(255) not null, name varchar(255) not null, primary key (id))
    ERROR : Table 'client' already exists
    
    Hibernate: create table reservation (id bigint not null auto_increment, state varchar(255), client_id bigint, primary key (id))
    Hibernate: create table restaurant (id bigint not null auto_increment, primary key (id))
    Hibernate: alter table reservation add constraint FKoewar6f18rkn4iptr6da4oysv foreign key (client_id) references client (id)
    
     INFO  : HHH000230: Schema export complete
     INFO  : Initialized JPA EntityManagerFactory for persistence unit 'default'
     INFO  : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
     INFO  : HHH000227: Running hbm2ddl schema export
    ERROR  : HHH000389: Unsuccessful: alter table `Reservation` drop foreign key `FK8gjvlt7rp8i9kp7rdmn107ni7`
    ERROR  : Can't DROP 'FK8gjvlt7rp8i9kp7rdmn107ni7'; check that column/key exists
    ERROR  : HHH000389: Unsuccessful: drop table if exists `Client`
    ERROR  : Cannot delete or update a parent row: a foreign key constraint fails
    ERROR  : HHH000389: Unsuccessful: create table `Client` (`id` bigint not null auto_increment, `createdOn` datetime, `credentialsVersion` integer not null, `hash` varchar(255), `username` varchar(255), `email` varchar(255) not null, `firstName` varchar(255) not null, `name` varchar(255) not null, primary key (`id`))
    ERROR  : Table 'client' already exists
     INFO  : HHH000230: Schema export complete
    
    @ManyToOne
    @JoinColumn(name = "client_id")
    private Client client;