3

I'm using Spring Boot 2 and Spring Data JPA using Hibernate 5.2. Spring or Hibernate (not sure, which one does it) can create DB tables automatically by setting spring.jpa.hibernate.ddl-auto = create. This automatically uses the connections settings from spring and the @Entity annotated classes to generate a schema for the DB platform I'm using and creates it.

I want to do the same but in code. I want to add a method that automatically uses existing classes and settings to generate the schema SQL and creates the tables. How to do that?

I tried StandardServiceRegistryBuilder and MetadataSources but I have to configure it manually. I assume there should be a way like Spring is doing it by itself.

Michael
  • 2,528
  • 3
  • 21
  • 54
  • Take a look at `org.hibernate.tool.hbm2ddl.SchemaUpdate` and `SchemaExport` classes (https://stackoverflow.com/a/14465841/3459206) – Alex Salauyou Jun 25 '18 at 11:15
  • I know of these methods but I need to tell them all the classes. This shoul be done automatically. If I'd need to modify it on every entity change, I could use a schema.sql. – Michael Jun 25 '18 at 12:27
  • 1
    I've found a solution in this answer: https://stackoverflow.com/a/34701374/3898725 – Michael Jun 26 '18 at 13:16

0 Answers0