Hibernate 在创建表之前尝试更改表
Posted
技术标签:
【中文标题】Hibernate 在创建表之前尝试更改表【英文标题】:Hibernate trying to alter table before creating it 【发布时间】:2020-08-11 12:55:19 【问题描述】:这里有一个简单的问题:我正在使用 mysql5Dialect 的 mySQL 数据库上运行 Spring Boot 2.2.5。在我向引用 User 实体的 Slide 实体添加 @ManyToOne 注释之前,一切都很顺利——现在 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
【问题讨论】:
我没有在您的“错误”中看到任何错误。 对不起,核心错误是 java.sql.SQLSyntaxErrorException: Table '32293814_alumnicemba.slides' doesn't exist 尝试再次运行该应用程序。它会工作得很好。并确保你有一个界面 SlideRepository 不,它不能正常工作。每次运行都会出现问题:Hibernate 尝试更改它尚未创建的表。是的,当然我有存储库接口。来吧。 【参考方案1】:发现问题:
private int order;
“order”在此处不允许作为字段名称,Hibernate 在尝试创建幻灯片表但未记录该错误时遇到错误。我已将该字段重命名为“排序”,现在可以使用了。
【讨论】:
以上是关于Hibernate 在创建表之前尝试更改表的主要内容,如果未能解决你的问题,请参考以下文章
Play + JPA + Hibernate + PostgreSQL:无法创建表
Spring Boot + Hibernate + Postgres - 不创建表