Initializing the database with Spring JPA and Hibernate

In this approach, most of the work is actually done by the Hibernate library and we merely set up the appropriate configurations and create conventionally expected files that are needed for Hibernate to do the work:

  • The spring.jpa.hibernate.ddl-auto=create-drop setting instructs Hibernate to use the @Entity models and, based on their structure, automatically deduces the database schema. Upon starting the application, the calculated schema will be used to preinitialize the database table structure; when the application is shut down, it will all be destroyed. Even in the event that the application was forcefully terminated or it abruptly crashed, upon startup, if the existing tables are detected, they will be dropped and recreated from scratch. So it's probably not a good idea to rely on this for a production environment.
If the spring.jpa.hibernate.ddl-auto property is not explicitly configured, Spring Boot uses create-drop for embedded databases such as H2 by default, so be careful and set it appropriately.
  • Hibernate expects that the import.sql file is residing in the root of the classpath. This is used to execute the declared SQL statements upon application startup. While any valid SQL statement can go in the file, it is recommended that you put in the data-importing statements such as INSERT or UPDATE and steer clear of table structure mutations, as the schema definition is already taken care of by Hibernate.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.113