Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate JPA2 Entities from Existing Database using Maven

Generate JPA 2 Entities from existing Database

I want to do same but using maven build.

Please suggest the plugin. As i googled it, i found the metamodel generation of JPA using JPA annotation entities.Didn't find anything related to this question.

like image 654
Shashi Avatar asked Feb 19 '13 11:02

Shashi


2 Answers

The official one to achieve this is hibernate-tools-maven-plugin. Here is a blog with a detailed explanation on how to integrate the plugin.

https://web.archive.org/web/20201013105933/https://jonamlabs.com/how-to-use-hibernate-tools-maven-plugin-to-generate-jpa-entities-from-an-existing-database/

like image 76
Manoj Reddy Avatar answered Sep 30 '22 04:09

Manoj Reddy


You could use the hibernate-tools-maven-plugin. In order to use it you can use the following configuration:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <h2.version>1.4.200</h2.version>
    <hibernate-tools-maven-plugin.version>5.4.11.Final</hibernate-tools-maven-plugin.version>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-tools-maven-plugin</artifactId>
        <version>${hibernate-tools-maven-plugin.version}</version>
        <dependencies>
          <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.2</version>
          </dependency>
          <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.2</version>
          </dependency>
          <dependency>
            <!-- DB Driver of your choice -->
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>Display Help</id>
            <phase>validate</phase>
            <goals>
              <goal>help</goal>
            </goals>
          </execution>
          <execution>
            <id>Entity generation</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>hbm2java</goal>
            </goals>
            <configuration>
              <ejb3>true</ejb3>
              <jdk5>true</jdk5>
            </configuration>
          </execution>
          <execution>
            <id>Schema generation</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>hbm2ddl</goal>
            </goals>
            <configuration>
              <delimiter>;</delimiter>
              <haltOnError>true</haltOnError>
              <format>true</format>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <revengFile>${project.basedir}/src/main/hibernate/hibernate.reveng.xml</revengFile>
          <configFile>${project.basedir}/src/main/hibernate/hibernate.cfg.xml</configFile>
          <detectManyToMany>true</detectManyToMany>
          <detectOneToOne>true</detectOneToOne>
          <detectOptimisticLock>true</detectOptimisticLock>
          <createCollectionForForeignKey>true</createCollectionForForeignKey>
          <createManyToOneForForeignKey>true</createManyToOneForForeignKey>
        </configuration>
      </plugin>
    </plugins>
  </build>

Run mvn generate-resources and you will find generated JPA Entities and a Schema DDL of the test.mv.db H2-Database (root folder from the project).

The connection configuration is in hibernate.cfg.xml file located in src/main/hibernate. In our case has the following content:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
    <property name="hibernate.connection.driver_class">org.h2.Driver</property>
    <property name="hibernate.connection.url">jdbc:h2:./test;DB_CLOSE_ON_EXIT=FALSE</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.connection.pool_size">1</property>
    <property name="hibernate.show_sql">true</property>
  </session-factory>
</hibernate-configuration>

We use the hibernate.reveng.xml file to customize the mapping type configuration, for example in order to use the java.time types we can use:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM
  "http://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>

  <type-mapping>
    <sql-type jdbc-type="DATE" hibernate-type="java.time.LocalDate"/>
    <sql-type jdbc-type="TIMESTAMP" hibernate-type="java.time.LocalDateTime"/>
  </type-mapping>

</hibernate-reverse-engineering>

The complete project is available over on Github.

like image 37
JuanMoreno Avatar answered Sep 30 '22 05:09

JuanMoreno