我對 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。設置上述屬性將覆蓋此行為。

萬千封印
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
,一旦您的模式創建工作,您需要更新您的列映射。
添加回答
舉報
0/150
提交
取消