Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot and Hibernate: print/log DDL

After I've added one or more classes with database mappings (JPA/hibernate), I would like Hibernate to print out the necessary schema updates so I can execute them on the database (through FlyWay for example). I do not want the updates to be executed automatically.

The only property that seems to give some control over this is the following

org.hibernate.tool.hbm2ddl=validate|update|create|create-drop|none

I don't want to update/change anything automatically. I want to set this to validate or none. When I do this, I don't get to see the generated schema.

I classical spring application, I used to use the hibernate SchemaExport class to print the DDL.

SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.execute(true, false, false, false);

Is there anything similar I can use in Spring Boot?

like image 309
bertvh Avatar asked Jul 30 '14 22:07

bertvh


People also ask

What is ddl in Hibernate?

Actually hbm2ddl Configuration means hibernate mapping to create schema DDL (Data Definition Language). <!– Drop and re-create the database schema on startup –>

How does Hibernate ddl-auto work?

hibernate. ddl-auto= create-drop" means that when the server is run, the database(table) instance is created. And whenever the server stops, the database table instance is droped.

What is ddl-Auto in Spring boot?

ddl-auto in Spring Boot configuration file to create or create-drop . If you set ddl-auto to create or create-drop, Hibernate will generate a schema for your entity based on its mapping. You need to add the following property in your application. properties file. spring.jpa.hibernate.ddl-auto=create.

Is @column a spring boot annotation?

Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. In this article, we will discuss how to change the column name in the Spring project using JPA. @Column annotation is used for Adding the column the name in the table of a particular MySQL database.


1 Answers

This is what I do...

First I make my entity changes, then I set these to:

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

Then

  1. re-run my app and let hibernate make the changes to the database.
  2. Go into the logs and copy the sql that hibernate used to update the db
  3. Paste that sql into a new Flyway script
  4. Shudown Boot App
  5. Drop the local database
  6. change ddl-auto back to validate
  7. Restart Boot App
  8. Test to make sure that Flyway made the correct updates. Hibernate and Flyway will now be in sync.
like image 134
Chris Savory Avatar answered Nov 09 '22 06:11

Chris Savory