我正在使用spring.jpa.hibernate.ddl-auto = update属性更新架构。
据我了解,如果我们在实体中进行更改,则表架构将得到更新。
但是在spring boot应用程序启动时,每次对外键执行alter命令时。
以下是实体。
@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命令?
答案 0 :(得分:1)
约束是数据库架构定义的一部分。 约束是对表的数据列实施的规则。这些用于限制可以进入表的数据类型。这样可以确保数据库中数据的准确性和可靠性。约束可以在列级别或表级别。列级约束仅应用于一列,而表级约束则应用于整个表。
种种约束条件:
问:休眠中的此约束是可选的还是取消以更新?
A:否。它不是可选的,对于关系实体是必需的。
如果需要,您还可以在数据库中定义此约束以在运行时进行更改,但不建议小心。
我认为这是冬眠的错误,它会根据以下链接每次更改(相同的问题):