Home > other >  DDL generation doesn't work with JPA approach
DDL generation doesn't work with JPA approach

Time:01-26

Hibernate version 6.1.6.FINAL is in use and I can generate the database (DDL) easily according to the mapping annotations and with the native Hibernate approach (hibernate.hbm2ddl.auto) as follows (persistence.xml):

<persistence-unit name="com.whatever.persistence.jpa">
    <description>
        A template for building a persistence layer/module.
    </description>
    <properties>
        <property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver" />
        <property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1" />
        <property name="jakarta.persistence.jdbc.user" value="sa" />
        <property name="jakarta.persistence.jdbc.password" value="" />

        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="create" />
    </properties>

</persistence-unit>

However, when I use the JPA approach (see code below) to generate the DDL, while the *.sql file was created properly the tables within the database are missing and that is my issue here.

<persistence-unit name="com.whatever.persistence.jpa">
    <description>
        A template for building a persistence layer/module.
    </description>
    <properties>
        <property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver" />
        <property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1" />
        <property name="jakarta.persistence.jdbc.user" value="sa" />
        <property name="jakarta.persistence.jdbc.password" value="" />

        <property name="hibernate.show_sql" value="true" />

        <property name="jakarta.persistence.schema-generation.scripts.action" value="create"/>
        <property name="jakarta.persistence.schema-generation.create-source" value="metadata"/>
        <property name="jakarta.persistence.schema-generation.scripts.create-target" value="tables.sql"/>
    </properties>

</persistence-unit>

CodePudding user response:

The subsequent property was missing so it creates the DDL within the database too.

 <property name="jakarta.persistence.schema-generation.database.action" value="create"/>

Refers to: Hibernate 6.1 User Guide

CodePudding user response:

You should add this property for database generation

<property name="jakarta.persistence.schema-generation.database.action" value="create"/>
  • Related