【发布时间】:2019-08-13 08:53:21
【问题描述】:
我在使用 Spring Boot 时遇到问题。我在资源文件夹中有一个 Schema.sql 和一个实体。在第一个应用程序运行时,一切都按预期工作。但是当我更改 schema.sql 中的列名、更新我的实体、删除数据库表并重新运行应用程序时,Spring 总是创建旧的实体列名。
在我的 application.properties 我有以下条目:
spring.datasource.name = mydatasource
spring.datasource.url = jdbc:mysql://localhost:3306/dbname?serverTimezone=UTC&createDatabaseIfNotExist=true
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.password = password
spring.datasource.username = username
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
security.oauth2.client.clientId= my_client
security.oauth2.resource.id= myid
security.oauth2.client.clientSecret= my_srcret
security.oauth2.client.accessTokenUri= http://localhost:8080/api/oauth/token
security.oauth2.client.userAuthorizationUri= http://localhost:8080/api/oauth/authorize
security.oauth2.resource.token-info-uri=http://localhost:8080/api/oauth/check_token
logging.level.org.springframework.web=DEBUG
我的新实体:
@Entity
@Table(name = "organizers")
public class Organizer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "commercialName")
private String commercialName;
@Column(name = "org_description")
private String description;
@Column(name = "verified")
private boolean verified;
@Column(name = "isOnline")
private boolean isOnline;
@Column(name = "org_type")
private OrganizerType type;
@Column(name = "alias")
private String alias;
@Column(name = "fone")
private String fone;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "userId")
private User user;
}
我的新 Schema.sql
create table if not exists organizers
(
id bigint PRIMARY KEY NOT NULL AUTO_INCREMENT,
userId varchar(256) not null,
commercialName varchar(100) not null,
description varchar(1000) not null,
verified boolean default 0,
isOnline boolean default 0,
type int not null,
alias varchar(100) not null,
fone varchar(100) not null,
constraint fk_user_id FOREIGN KEY (userId) REFERENCES users (username)
);
这是 Spring 创建的屏幕截图:
正如您在图片中看到的,Spring 总是根据我的旧模式和实体创建组织者表!我正在使用 Spring Boot 2.1.6 版和 MySql 8.0.17 版
我认为 Spring Boot 总是缓存我的旧 Schema.sql 或我的旧实体!可能是什么问题?
【问题讨论】:
-
希望你的项目中只有一个 Schema.sql。
-
是的,我只有一个
-
是
org_type专栏的问题吗? -
一般来说没有。我通过设置 jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 解决了这个问题并删除了 dll-auto
标签: java mysql spring hibernate spring-boot