使用Maven从现有数据库生成JPA2实体

时间:2013-02-19 11:43:43

标签: hibernate maven jpa

Generate JPA 2 Entities from existing Database

我想做同样但使用maven build。

请推荐该插件。当我用Google搜索它时,我发现使用JPA注释实体生成JPA的元模型。没有找到与此问题相关的任何内容。

4 个答案:

答案 0 :(得分:5)

您正在使用休眠时,默认选项为hibernate3-maven-plugin,更具体地说是hibernate3:hbm2java使用<ejb3>true</ejb3>配置的javax.persistence目标。它将生成带注释的pojos(大多数注释来自标准org.hibernate.annotations包,但它也可能包含自定义{{1}})。

John Citizen's answer查看JBoss Community以了解示例配置。

答案 1 :(得分:3)

答案 2 :(得分:2)

您可以使用hibernate-tools-maven-plugin。为了使用它,您可以使用以下配置:

  <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>

运行mvn generate-resources,您将找到生成的JPA实体和test.mv.db H2-Database(项目的根文件夹)的Schema DDL。

连接配置位于hibernate.cfg.xml中的src/main/hibernate文件中。在我们的案例中具有以下内容:

<?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>

我们使用hibernate.reveng.xml文件来自定义映射类型配置,例如,为了使用java.time类型,我们可以使用:

<?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>

可以在Github上找到完整的项目。

答案 3 :(得分:0)

要实现这一目标的官方机构是hibernate-tools-maven-plugin。这是一个博客,其中包含有关如何集成插件的详细说明。

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