gpt4 book ai didi

java - Hibernate 在创建表之前尝试更改表

转载 作者:行者123 更新时间:2023-12-02 02:42:20 24 4
gpt4 key购买 nike

此处的简单问题:我在使用 MySQL5Dialect 的 mySQL 数据库上运行 Spring Boot 2.2.5。一切都很顺利,直到我将 @ManyToOne 注释添加到引用 User 实体的 Slide 实体 - 现在 Hibernate 无法创建表,因为它创建了用户表,然后尝试更改它尚未创建的幻灯片表。我做错了什么?

用户:

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private boolean enabled;
private String token;
private String username;
private String password;
private String role;
private String name;
private String surname;
private String email;
private boolean emailVisible;
private String phone;
private boolean phoneVisible;
private int cohortNumber;
private String company;
private String position;
private String linkedIn;
private String function;
private String bio;
private String membership;
private Date membershipSince;
private Date membershipUntil;
}

幻灯片:

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString(exclude = "editor")
@Table(name = "slides")
public class Slide {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

private boolean visible;
private int order;
private Date edited;

@ManyToOne
@JoinColumn(name = "editor_id")
private User editor;

private String title;
private String text;
private String picture;
}

Hibernate 配置细节:

spring.jpa.hibernate.ddl-auto = update
spring.datasource.initialization-mode=always
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

错误:

Hibernate: create table users (id integer not null auto_increment, bio varchar(255), cohort_number integer not null, company varchar(255), email varchar(255), email_visible bit not null, enabled bit not null, function varchar(255), linked_in varchar(255), membership varchar(255), membership_since datetime, membership_until datetime, name varchar(255), password varchar(255), phone varchar(255), phone_visible bit not null, position varchar(255), role varchar(255), surname varchar(255), token varchar(255), username varchar(255), primary key (id)) engine=MyISAM
Hibernate: alter table slides add constraint FKobqxptfxg36ls278o63ouq369 foreign key (editor_id) references users (id)
2020-08-11 14:27:48.201 WARN 8224 --- [ restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL "alter table slides add constraint FKobqxptfxg36ls278o63ouq369 foreign key (editor_id) references users (id)" via JDBC Statement
...
Caused by: java.sql.SQLSyntaxErrorException: Table '32293814_alumnicemba.slides' doesn't exist

最佳答案

发现问题:

私有(private)订单;

此处不允许将“order”作为字段名称,并且 Hibernate 在尝试创建幻灯片表但未记录该错误时遇到错误。我已将该字段重命名为“排序”,现在可以使用了。

关于java - Hibernate 在创建表之前尝试更改表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63358602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com