How to configure hbm2java and hbm2dao to add packagename to generated classes

自作多情 提交于 2019-11-28 07:05:38

Just in case, here is a working configuration of the hibernate3-maven-plugin for a bottom-up approach:

  <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>hbm2hbmxml</goal>
          <goal>hbm2cfgxml</goal>
        </goals>
      </execution>
      <execution>
        <id>generate-entities</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>hbm2java</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <components>
        <component>
          <name>hbm2hbmxml</name>
          <implementation>jdbcconfiguration</implementation>
          <outputDirectory>target/classes</outputDirectory>
        </component>
        <component>
          <name>hbm2cfgxml</name>
          <implementation>jdbcconfiguration</implementation>
          <outputDirectory>target/classes</outputDirectory>
        </component>
        <component>
          <name>hbm2java</name>
          <implementation>configuration</implementation>
          <outputDirectory>target/generated-sources/hibernate3</outputDirectory>
        </component>
      </components>
      <componentProperties>
        <propertyfile>src/main/resources/database.properties</propertyfile>
        <jdk5>true</jdk5>
        <ejb3>false</ejb3>
        <packagename>com.mycompany.myapp</packagename>
        <format>true</format>
        <haltonerror>true</haltonerror>
      </componentProperties>
    </configuration>
    <dependencies>
      <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.5.3.0_1</version>
      </dependency>
    </dependencies>
  </plugin>

And here is the content of my src/main/database.properties file:

hibernate.connection.driver_class=org.apache.derby.jdbc.EmbeddedDriver
hibernate.connection.url=jdbc:derby:./derbyDBs/EMPLDB
hibernate.connection.username=APP
hibernate.connection.password=APP
hibernate.dialect=org.hibernate.dialect.DerbyDialect

#workaround for http://opensource.atlassian.com/projects/hibernate/browse/HBX-1145
hibernate.connection.autocommit=true 

This setup:

  1. Generates *.hbm.xml in target/classes (with the package) during generate-resources.
  2. Generates a hibernate.cfg.xml in target/classes with entries for the mapping files.
  3. Generates entities from the mappings in target/generated-sources/hibernate3 (I recommend following the target/generated-sources/<tool> convention for generated sources so that they will get auto-detected by IDEs).

Here is a the result of clean compile against a sample database with two tables:

$ mvn clean compile
...
$ tree target/
target/
├── classes
│   ├── com
│   │   └── mycompany
│   │       └── myapp
│   │           ├── Department.class
│   │           ├── Department.hbm.xml
│   │           ├── Employee.class
│   │           └── Employee.hbm.xml
│   ├── database.properties
│   └── hibernate.cfg.xml
└── generated-sources
    └── hibernate3
        └── com
            └── mycompany
                └── myapp
                    ├── Department.java
                    └── Employee.java
mmm

Ok, I figured it out. I put the answer here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!