【问题标题】:Generate JPA2 Entities from Existing Database using Maven使用 Maven 从现有数据库生成 JPA2 实体
【发布时间】:2013-02-04 02:03:19
【问题描述】:

Generate JPA 2 Entities from existing Database

我也想做同样的事情,但使用 maven 构建。

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

【问题讨论】:

标签: hibernate maven jpa


【解决方案1】:

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

查看John Citizen's answerJBoss Community 以获取示例配置。

【讨论】:

  • 有没有 eclipselink 的方式来做到这一点? netbeans/eclipse 在内部做了什么?
  • Kalpesh,实际上您也可以使用 hibernate maven 插件(它将使用这些设置生成干净的 JPA 实体)。我不确定 eclipselink 是否有特定的 maven 工具(这是一个好问题)。我也不确定 Dali 和 Netbeans 在内部做什么。
【解决方案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。

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

<?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】:
    【解决方案4】:

    【讨论】:

    • 不幸的是,wikispaces 不再运行。
    • 你知道其他类似的工具吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2011-08-15
    • 2015-08-04
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多