Ensure that hbm2java creates Entity classes that use jakarta.persistence rather than javax.persistence?

368 Views Asked by At

When I run hbm2java I would like it to create Entity classes that:

import jakarta.persistence

rather than (as currently)

import javax.persistence

I am invoking hbm2java from a Maven build thus:

<plugin>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-tools-maven-plugin</artifactId>
    <version>5.6.15.Final</version>
    <executions>
        <execution>
           <phase>generate-sources</phase>
           <goals>
               <goal>hbm2java</goal>
           </goals>
        </execution>
     </executions>
     <configuration>
         <packageName>PACKAGE_NAME</packageName>
         <outputDirectory>OUTPUT_DIR</outputDirectory>
         <ejb3>true</ejb3>
     </configuration>
    <dependencies>
       <dependency>
           <groupId>com.mysql</groupId>
           <artifactId>mysql-connector-j</artifactId>
           <version>8.0.32</version>
       </dependency>
       <!-- I thought the following dependency would work but it didnt.. -->
       <!--<dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-jpamodelgen-jakarta</artifactId>
           <version>5.6.15.Final</version>
       </dependency>-->
   </dependencies>
</plugin>

With hibernate.properties

hibernate.connection.url=jdbc:mysql://someserver
hibernate.connection.username=username
hibernate.connection.password=password
hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQLDialect
2

There are 2 best solutions below

0
On

it works with hbernate-tools-maven. I use the latest version on maven central: 6.2.6.Final This is the definition of the plugin tag inside my pom.xml

   <plugin>
            <groupId>org.hibernate.tool</groupId>
            <artifactId>hibernate-tools-maven</artifactId>
            <version>6.2.6.Final</version>
            <configuration>
                <packageName>com.example.entity</packageName>
                <revengFile>src/main/resources/hibernate.reveng.xml</revengFile>
                <outputDirectory>src/main/java</outputDirectory>
                <ejb3>true</ejb3>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate.tool</groupId>
                    <artifactId>hibernate-tools-orm</artifactId>
                    <version>6.2.6.Final</version>
                </dependency>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                    <version>2.1.214</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>entity-generation</id>
                    <phase>none</phase>
                    <goals>
                        <goal>hbm2java</goal>
                    </goals>
                </execution>
            </executions>
    </plugin>

hibernate.properties

hibernate.dialect = org.hibernate.dialect.H2Dialect
hibernate.connection.driver_class = org.h2.Driver
hibernate.connection.url = jdbc:h2:file:~/my-db;MODE=MySQL
hibernate.connection.username = sa
hibernate.connection.password =
hibernate.hbm2ddl.auto = update

hibernate.reveng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC
    "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>

   <schema-selection match-catalog="CAMUNDA-DB" match-schema="PUBLIC"/>
   <type-mapping>
       <!-- jdbc-type is name for java.sql.Types -->
       <sql-type jdbc-type="VARCHAR" hibernate-type="java.lang.String" />
       <sql-type jdbc-type="DOUBLE"  precision='4' hibernate- 
        type="java.lang.Double" />
       <sql-type jdbc-type="TIMESTAMP"  hibernate- 
        type="java.time.LocalDateTime" />
   </type-mapping>
   <table-filter match-name="flyway_schema_history" exclude="true"/>

</hibernate-reverse-engineering>

my command line for generating entities classes:

mvn hibernate-tools:hbm2java
0
On

I've been searching for this as well to find groupId org.hibernate.tool artifactId hibernate-tools-maven version 6.1.7.Final doing it