13

既存のデータベースからJPA2エンティティを生成する

同じことをしたいのですが、Mavenビルドを使用しています。

プラグインを提案してください。グーグルで検索したところ、JPAアノテーションエンティティを使用したJPAのメタモデル生成が見つかりました。この質問に関連するものは見つかりませんでした。

4

4 に答える 4

5

hibernateを使用しているため、デフォルトのオプションはhibernate3-maven-pluginになります。具体的には、でhibernate3:hbm2java構成された目標<ejb3>true</ejb3>です。注釈付きのpojoが生成されます(ほとんどの注釈は標準javax.persistenceパッケージからのものですが、カスタムも含まれる場合がありますorg.hibernate.annotations)。

サンプル構成については、JBossCommunityでJohnCitizenの回答を確認してください。

于 2013-06-14T17:14:15.660 に答える
4

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(プロジェクトのルートフォルダー)のスキーマ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で入手できます。

于 2020-01-14T02:22:04.367 に答える
4

これを実現するための公式なものは、hibernate-tools-maven-pluginです。これは、プラグインを統合する方法の詳細な説明が記載されたブログです。

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

于 2020-08-31T17:21:23.957 に答える
3

MinuteProjectを試してみてください:(Mavenプロジェクトを生成します)

http://minuteproject.wikispaces.com/

http://javacodesamples.wordpress.com/2011/02/04/jpa2-reverse-engineering-tool/

于 2013-06-14T15:20:22.817 に答える