Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring.jpa.hibernate.ddl-auto=update property alters the foreign key every time

I am using spring.jpa.hibernate.ddl-auto=update property to update the schema.

As per my understanding if we do changes in the entity then table schema gets updated.

But on spring boot app startup every time alter command gets executed for the foreign key.

Following is the entity.

@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;

}

In entity you can see I have following two property for that foreign key gets created on spring boot app starts first time:

@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;

So when I am restarting the app or saving the code every time foreign key constraints gets altered even if I am not changing that relationship(property).

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)

So what should I change to not execute alter command for the foreign key if I do not change the foreign key entity property?

like image 943
stackUser Avatar asked Nov 30 '18 05:11

stackUser


People also ask

What does Spring JPA Hibernate DDL Auto do?

spring. jpa. hibernate. ddl-auto (enum) is a Hibernate feature that controls the behavior in a more fine-grained way.

What does hibernate hbm2ddl auto property helps us with?

hbm2ddl. auto Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.

How do I stop a table from hibernation?

Just set the hibernate. hbm2ddl. auto property to none instead of update since you don't need the DB schema to be generated for you.


1 Answers

Constraints are part of a database schema definition. Constraints are the rules enforced on the data columns of a table. These are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the database. Constraints could be either on a column level or a table level. The column level constraints are applied only to one column, whereas the table level constraints are applied to the whole table.

kinds of constraints are:

  • NOT NULL− Ensures that a column cannot have NULL value.
  • DEFAULT - Provides a default value for a column when none is specified.
  • UNIQUE - Ensures that all values in a column are different.
  • PRIMARY KEY - Uniquely identifies each row/record in a database table.
  • FOREIGN KEY - Uniquely identifies a row/record in any of the given database table.
  • CHECK CONSTRAINT - The CHECK constraint ensures that all the values in a column satisfies certain conditions.
  • INDEX - Used to create and retrieve data from the database very quickly.

Q : Is this constraint in hibernate optional or cancel to update ?

A : No. it is not optional, it is needed for relational entity.

You can also define this constraint in your DB to changes while run time if necessary but be careful not recommended.

I think this is a bug for hibernate to alter every time base on this below link (same problem):

https://discourse.hibernate.org/t/manytoone-alter-table-query-is-generating-every-time-when-inserting-a-value/1162/6

like image 74
MostafaMashayekhi Avatar answered Sep 22 '22 19:09

MostafaMashayekhi