Spring boot and Hibernate: print / log DDL

After adding one or more classes with database mappings (JPA / hibernate), I would like Hibernate to print the necessary schema updates, so I can execute them in the database (for example, via FlyWay). I do not want updates to be automatic.

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 do not want to automatically update / change anything. I want to set this to check or not. When I do this, I cannot see the generated circuit.

I am a classic spring application, I used the hibernate SchemaExport class to print DDL.

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

Is there anything similar I can use in spring boot?

+8
java spring spring-boot hibernate jpa
source share
2 answers

This is what I do ...

First I make my entities change, and then set them like this:

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

Then

  • run my application and let hibernate make changes to the database.
  • Log in and copy sql which sleep mode is used to update db
  • Paste this sql into a new span script
  • Shudown Download App
  • Drop local database
  • change ddl-auto back to check
  • Restart the application to download
  • Test to make sure that Flyway has made the correct updates. Hibernate and Flyway will now sync.
+10
source share

The show-sql installation solution did not work for me even with debugging turned on, so I ended up writing and running this simple class.

 public class SchemaExporter{ public static org.hibernate.cfg.Configuration getConfiguration() { org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration(); ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class)); for (BeanDefinition bd : scanner.findCandidateComponents("com.package.where.my.entitybeans.are")) { String name = bd.getBeanClassName(); try { System.out.println("Added annotated entity class " + bd.getBeanClassName()); cfg.addAnnotatedClass(Class.forName(name)); } catch (Exception e) { e.printStackTrace(); } } cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); cfg.setProperty("hibernate.show_sql", "true"); cfg.setProperty("hibernate.format_sql", "true"); cfg.setProperty("hibernate.hbm2ddl.auto", "update"); cfg.setProperty("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy"); cfg.setProperty("hibernate.connection.url", CONNECTIONURL); cfg.setProperty("hibernate.connection.username", USERNAME); cfg.setProperty("hibernate.connection.password", PWD); cfg.setProperty("hibernate.connection.driver", DRIVER); return cfg; } public static void main(String[] args) { SchemaExport export = new SchemaExport(getConfiguration()); export.setDelimiter(";"); export.setHaltOnError(true); export.setFormat(true); export.create(true,true); } } 

By running it, I see DDL in the console and continue as Chris suggested

0
source share

All Articles