【发布时间】:2019-05-02 06:43:19
【问题描述】:
我正在使用 spring.jpa.hibernate.ddl-auto=update 属性来更新架构。
根据我的理解,如果我们对实体进行更改,那么表架构就会更新。
但是在每次为外键执行 alter 命令时,在 spring boot 应用程序启动时。
以下是实体。
@Entity
@Table(name = "feedback")
@Data
public class Feedback implements Serializable {
private static final long serialVersionUID = -6420805626682233375L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "study_id")
@JsonIgnore
private Study study;
@ManyToOne(fetch= FetchType.EAGER)
@JoinColumn(name="user_id", nullable = false)
private User user;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "feedback_date", nullable = false)
private Date feedbackDate;
@Size(max = 1000)
@Column(name = "feedback", length = 1000)
private String feedback;
}
在实体中,您可以看到我有以下两个属性,用于在 spring boot 应用程序首次启动时创建外键:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "study_id")
@JsonIgnore
private Study study;
@ManyToOne(fetch= FetchType.EAGER)
@JoinColumn(name="user_id", nullable = false)
private User user;
因此,当我每次重新启动应用程序或保存代码时,即使我没有更改该关系(属性),外键约束也会发生变化。
2018-12-05 18:44:12.027 INFO 22736 --- [ restartedMain] c.d.smartviewer.SmartViewerApplication : Starting SmartViewerApplication on LAPTOP-F95LLCU3 with PID 22736 (D:\Sagar_\SVN\SmartViewer\target\classes started by ASUS in D:\Sagar_\SVN\SmartViewer)
2018-12-05 18:44:12.027 DEBUG 22736 --- [ restartedMain] c.d.smartviewer.SmartViewerApplication : Running with Spring Boot v2.0.6.RELEASE, Spring v5.0.10.RELEASE
2018-12-05 18:44:12.027 INFO 22736 --- [ restartedMain] c.d.smartviewer.SmartViewerApplication : No active profile set, falling back to default profiles: default
2018-12-05 18:44:13.356 INFO 22736 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1329 ms
Hibernate: alter table annotation add constraint FK7hwy1g5myfk7grmm2j7faqggd foreign key (parent_id) references annotation (id)
Hibernate: alter table feedback add constraint FKfxt8nk3jikofi3x40bsjd00vt foreign key (study_id) references study (id)
Hibernate: alter table feedback add constraint FK7k33yw505d347mw3avr93akao foreign key (user_id) references user (id)
Hibernate: alter table hospital add constraint FK3922fhj7qnyc3bw5x8xl6m6xc foreign key (contact_1) references contact (id)
如果我不更改外键实体属性,我应该更改什么以不执行外键的alter命令?
【问题讨论】:
-
你能详细解释一下吗?
-
请查看更新后的问题了解更多详情。
-
请查看我的回答
-
你找到答案了吗?
标签: spring hibernate spring-boot jpa