6

Hibernate ツールと構成について次の質問があります。次のようなリバース エンジニアリングを使用して、データベースから JPA クラスを生成するように hibernate を構成します。

hibernate.cfg.xml

 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
   <session-factory>
       <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
       <property    name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
       <property name="hibernate.connection.password">pass</property>
       <property name="hibernate.connection.url">jdbc:oracle:thin:url</property>
       <property name="hibernate.connection.username">user</property>
       <property name="hibernate.default_schema">schema</property>
       <property     name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
    </session-factory>
  </hibernate-configuration>

次に、設定をリバース エンジニアリングする必要があります。

hibernate.reveng.xml

<hibernate-reverse-engineering>
  <table-filter match-name="TB1"/>
    <table name="TB_1" class="com.classtb1">
     <column name="ENDPAGE" property="pageIntervalEnd"/>
     <column name="SELECTABLE" property="selectableInd"/>
  </table>
</hibernate-reverse-engineering>

これは、このリバース エンジニアリング構成ファイルでマップされる 1 つのテーブルのサンプルです。

デフォルトでは、エンティティからのすべての関係は次のように生成されます。

  @Entity
  @Table(name="TB1"
  )
  public class Classtb1  implements java.io.Serializable {
   ...
        private Set<Classtb1Entry> classtb1= new HashSet<Classtb1Entry>(0);
   ...
       @OneToMany(fetch=FetchType.LAZY, mappedBy="Classtb1")
    public Set<Classtb1Entry> getClasstb1Entries() {
      return this.classtb1Entries;
    }
  }

maven pom.xml ファイルは次のようになります。

  <profiles>
  <profile>
    <id>WithoutDBGen</id>
  </profile>
  <profile>
   <id>Full</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>2.2</version>
        <executions>
          <execution>
            <id>generate-xml-files</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>hbm2cfgxml</goal>
            </goals>
            <inherited>false</inherited>
            <configuration>
              <components>
                <component>
                  <name>hbm2cfgxml</name>
                  <implementation>jdbcconfiguration</implementation>
                </component>
              </components>
              <componentProperties>
                <packagename>com.persistence.jpa</packagename>
                <revengfile>src/main/resources/hibernate.reveng.xml</revengfile>
              </componentProperties>
            </configuration>
          </execution>

          <execution>
            <id>generate-hbm-xml-files</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>hbm2hbmxml</goal>
            </goals>
            <inherited>false</inherited>
            <configuration>
              <components>
                <component>
                  <name>hbm2hbmxml</name>
                  <outputDirectory>target/classes</outputDirectory>
                </component>
              </components>
              <componentProperties>
                <packagename>com.persistence.jpa</packagename>
                <revengfile>src/main/resources/hibernate.reveng.xml</revengfile>
                <detectoptmisticlock>false</detectoptmisticlock>
              </componentProperties>
            </configuration>
          </execution>

          <execution>
            <id>generate-jpa-entities</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>hbm2java</goal>
            </goals>
            <inherited>false</inherited>
            <configuration>
              <components>
                <component>
                  <name>hbm2java</name>
                  <implementation>annotationconfiguration</implementation>
                  <outputDirectory>src/main/generated</outputDirectory>
                </component>
              </components>
              <componentProperties>
                <packagename>com.persistence.jpa</packagename>
                <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
                <templatepath>target/hibernate3/generated-mappings/</templatepath>
                <ejb3>true</ejb3>
                <jdk5>true</jdk5>
              </componentProperties>
            </configuration>
          </execution>

          <execution>
            <id>generate-dao</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>hbm2dao</goal>
            </goals>
            <inherited>false</inherited>
            <configuration>
              <components>
                <component>
                  <name>hbm2dao</name>
                  <implementation>annotationconfiguration</implementation>
                  <outputDirectory>src/main/generated</outputDirectory>
                </component>
              </components>
              <componentProperties>
                <packagename>com.persistence.dao</packagename>
                <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
                <templatepath>target/hibernate3/generated-mappings/</templatepath>
                <ejb3>true</ejb3>
                <jdk5>true</jdk5>
              </componentProperties>
            </configuration>
          </execution>

        </executions>

      </plugin>
    </plugins>
    </build>
    </profile>
   </profiles>

TB1 の場合のように、1 つのリレーションをどこで設定して、代わりに取得できますか

@OneToMany(fetch=FetchType.LAZY

i want to generate 

@OneToMany(fetch=FetchType.EAGER ?

私の場合、このオプションはどこで設定できますか?

私が抱えている2番目の問題は、DAO生成に関するものです.DAOクラスはデフォルトで@Statelessとして注釈が付けられています.これを指定して、DAOに @Repository のような別の注釈を付けるか、少なくとも注釈を付けないようにするにはどうすればよいですか?

4

1 に答える 1

1

休止状態でこのフォーラム リンクを見つけました: https://forum.hibernate.org/viewtopic.php?f=1&t=1003635 リバース エンジニアリング構成ファイルでこの動作を構成する可能性はないようですが、本当に悲しいことです。リバース エンジニアリングによって hbm.xml ファイルが生成されます。ファイル hbm.xml では、ロード オプション lazy(lazy="true") またはeager(lazy="false") を構成できますが、前は構成できません。おそらく、生成された hbm.xml を変更する Maven タスクがここで機能するでしょう。

誰かがこれを本当に感謝する他の考えを持っているなら、私はまだ可能性を探しています.

于 2012-02-27T13:09:03.140 に答える