为了账号安全,请及时绑定邮箱和手机立即绑定

旧实体版本的 Spring Boot 参考

旧实体版本的 Spring Boot 参考

蝴蝶刀刀 2023-06-04 10:25:04
我对 Spring Boot 有疑问。我在资源文件夹和一个实体中有一个 Schema.sql。在第一次运行应用程序时,一切都按预期工作。但是当我更改 schema.sql 中的列名、更新我的实体、删除数据库表并重新运行应用程序时,Spring 始终创建旧的实体列名。在我的 application.properties 中,我有以下条目:spring.datasource.name                   = mydatasourcespring.datasource.url               = jdbc:mysql://localhost:3306/dbname?serverTimezone=UTC&createDatabaseIfNotExist=truespring.datasource.driver-class-name      = com.mysql.cj.jdbc.Driverspring.datasource.password               = passwordspring.datasource.username               = usernamespring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialectspring.jpa.show-sql=falsespring.jpa.hibernate.ddl-auto=updatesecurity.oauth2.client.clientId= my_clientsecurity.oauth2.resource.id= myidsecurity.oauth2.client.clientSecret= my_srcretsecurity.oauth2.client.accessTokenUri= http://localhost:8080/api/oauth/tokensecurity.oauth2.client.userAuthorizationUri= http://localhost:8080/api/oauth/authorizesecurity.oauth2.resource.token-info-uri=http://localhost:8080/api/oauth/check_tokenlogging.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;}
查看完整描述

2 回答

?
守候你守候我

TA贡献1802条经验 获得超10个赞

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

默认情况下,spring 使用 org.springframework.boot.orm.jpa.SpringNamingStrategy。这会将 commercialName(驼峰式)之类的任何内容转换为 commerical_name。设置上述属性将覆盖此行为。


查看完整回答
反对 回复 2023-06-04
?
万千封印

TA贡献1891条经验 获得超3个赞

spring.jpa.hibernate.ddl-auto=update

设置为update以便休眠更新您的模式,它应该被完全删除,以便 flyway 可以从创建模式schema.sql

此外,您需要添加以下配置以启用从 flyway 创建模式:

spring.datasource.initialization-mode=always

从文档数据库初始化

在基于 JPA 的应用程序中,您可以选择让 Hibernate 创建架构或使用 schema.sql,但您不能同时执行这两项操作。如果您使用 schema.sql,请确保禁用 spring.jpa.hibernate.ddl-auto。

此外,您的实体映射存在错误。新实体引用该列@Column(name = "org_description"),但在您的新模式定义中,该列被称为 just description,一旦您的模式创建工作,您需要更新您的列映射。


查看完整回答
反对 回复 2023-06-04
  • 2 回答
  • 0 关注
  • 100 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信