首页 > 解决方案 > 旧实体版本上的 Spring Boot 参考

问题描述

我在使用 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 总是根据我的旧模式和实体创建组织者表!我正在使用带有 MySql 版本 8.0.17 的 Spring Boot 版本 2.1.6

我认为 Spring Boot 总是缓存我的旧 Schema.sql 或我的旧实体!问题可能是什么?

标签: javamysqlspringhibernatespring-boot

解决方案


它没有引用旧的实体版本,它看起来就是这样。在应用程序属性中设置 spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy。

默认情况下,spring 使用 org.springframework.boot.orm.jpa.SpringNamingStrategy。这会将诸如商业名称(在骆驼情况下)之类的任何内容转换为商业名称。设置上述属性将覆盖此行为。


推荐阅读