Hibernate尝试在创建表之前修改它。

huangapple go评论77阅读模式
英文:

Hibernate trying to alter table before creating it

问题

这里有一个简单的问题:我正在运行Spring Boot 2.2.5,使用MySQL5Dialect连接到一个MySQL数据库。一切都很顺利,直到我向Slide实体添加了@ManyToOne注解,引用了User实体 - 现在Hibernate无法创建表,因为它首先创建了users表,然后尝试修改slides表,但它还没有创建。我做错了什么?

用户:

@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
英文:

Simple issue here: I'm running Spring Boot 2.2.5 on a mySQL database with MySQL5Dialect. Everything was peachy until I've added @ManyToOne annotation to Slide entity referencing the User entity - now Hibernate can't create tables because it creates the users table and then tries to alter slides table which it hasn't created yet. What did I do wrong?

User:

@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;
}

Slide:

@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 config specifics:

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

And the error:

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

答案1

得分: 5

"Found the problem:

private int order;

"order" is not allowed as a field name here and Hibernate was encountering an error when trying to create the slides table but not logging that error. I've renamed the field to "sorting" and it works now."

英文:

Found the problem:

private int order;

"order" is not allowed as a field name here and Hibernate was encountering an error when trying to create the slides table but not logging that error. I've renamed the field to "sorting" and it works now.

huangapple
  • 本文由 发表于 2020年8月11日 20:55:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63358602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定