首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hibernate工具,逆向工程

Hibernate工具,逆向工程
EN

Stack Overflow用户
提问于 2012-02-24 22:59:45
回答 1查看 9K关注 0票数 5

关于Hibernate工具和配置,我有以下问题:我配置hibernate,使用逆向工程从数据库生成JPA类,如下所示:

hibernate.cfg.xml

代码语言:javascript
复制
 <?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

代码语言:javascript
复制
<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>

这是在此反向工程配置文件中映射的一个表的示例。

现在,默认情况下,来自实体的所有关系都是这样生成的:

代码语言:javascript
复制
  @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文件如下所示:

代码语言:javascript
复制
  <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的情况一样,要获取而不是

代码语言:javascript
复制
@OneToMany(fetch=FetchType.LAZY

i want to generate 

@OneToMany(fetch=FetchType.EAGER ?

在我的情况下,我可以在哪里配置此选项?

我遇到的第二个问题是DAO生成,默认情况下,DAO类被注释为@Stateless,我如何指定它才能让我的DAO使用另一个注释,比如@Repository,或者至少没有注释?

EN

回答 1

Stack Overflow用户

发布于 2012-02-27 21:09:03

我刚刚在hibernate上找到了这个论坛链接:https://forum.hibernate.org/viewtopic.php?f=1&t=1003635似乎没有可能在反向工程配置文件中配置这个行为,这真的很可悲。逆向工程是生成hbm.xml文件。在文件hbm.xml中,您可以配置加载选项lazy(lazy="true")或eager(lazy="false"),但不能在此之前配置。也许让一个maven任务来修改生成的hbm.xml在这里是可行的。

如果任何人有任何其他想法,我真的会欣赏这个,我仍然在寻找一种可能性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9432864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档