Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate tool, reverse engineering

I have the following question about Hibernate tools and the configuration : I configure the hibernate to genrate JPA classes from the database with reverse engineering like this :

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>

then I have for reverse engineering the config:

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>

This is a sample of one table what is mapped in this reverse engineering configuration file.

Now by default all relations from the entities are generated like this :

  @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;
    }
  }

The maven pom.xml file looks like this :

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

Where can I configure for one relation like in the case of TB1 to get instead of

@OneToMany(fetch=FetchType.LAZY

i want to generate 

@OneToMany(fetch=FetchType.EAGER ?

Where I can configure this option in my case ?

Second problem I have is with the DAO generation, the DAO classes are annotated by default as @Stateless, how can I specify this so my DAO are annotated with another annotation like @Repository or at least no annotation ?

like image 837
Mirea Vasile Avatar asked Nov 14 '22 09:11

Mirea Vasile


1 Answers

I just found this forum link on hibernate: https://forum.hibernate.org/viewtopic.php?f=1&t=1003635 Seems there is no possibility to configure this behavior in the reverse engineering config file, what is really sad. The reverse engineering is generating the hbm.xml files. In the file hbm.xml you can configure the loading option lazy(lazy="true") or eager(lazy="false") but not before. Maybe having a maven task to modify the generated hbm.xml would work here.

If any one have any other idea I would really apreciate this, I am still searching for a possibility.

like image 96
Mirea Vasile Avatar answered Feb 22 '23 23:02

Mirea Vasile