如何配置 hbm2java 和 hbm2dao 将包名添加到生成的类中

发布于 2024-09-02 00:18:41 字数 2541 浏览 6 评论 0原文

我正在尝试使用 Maven 配置 hbm2java 来生成 POJO 类和 DAO 对象。我正在处理的问题之一是未生成包名称。我为此使用以下 pom:

<execution>
    <id>hbm2java</id>
    <phase>generate-sources</phase>
    <goals>
        <goal>hbm2java</goal>
    </goals>
    <inherited>false</inherited>
    <configuration>
        <components>
            <component>
                <name>hbm2java</name>
                <implementation>configuration</implementation>
            </component>
        </components>
        <componentProperties>
            <packagename>package.name</packagename>
            <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
        </componentProperties>
    </configuration>
</execution>

然而生成的代码以以下内容开头:

// default package
// Generated 2010-05-17 13:11:51 by Hibernate Tools 3.2.2.GA

/**
 * Messages generated by hbm2java
 */
public class Messages  implements java.io.Serializable {

有没有办法强制 maven 生成 packagename 中定义的 package 部分?

更新:

这是我的 hibernate.cfg.xml,也是由 hibernate-tools (hbm2cfgxml) 自动生成的:

<?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.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">1800</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/db</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <mapping resource="package/name/Messages.hbm.xml" />
    </session-factory>
</hibernate-configuration>

I'm trying to configure hbm2java with maven to generate POJO classes and DAO objects. One of the issues I'm dealing with is package names aren't generated. I'm using the following pom for that:

<execution>
    <id>hbm2java</id>
    <phase>generate-sources</phase>
    <goals>
        <goal>hbm2java</goal>
    </goals>
    <inherited>false</inherited>
    <configuration>
        <components>
            <component>
                <name>hbm2java</name>
                <implementation>configuration</implementation>
            </component>
        </components>
        <componentProperties>
            <packagename>package.name</packagename>
            <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
        </componentProperties>
    </configuration>
</execution>

Yet the generated code begins with the following:

// default package
// Generated 2010-05-17 13:11:51 by Hibernate Tools 3.2.2.GA

/**
 * Messages generated by hbm2java
 */
public class Messages  implements java.io.Serializable {

Is there a way to force maven to generate the package part as defined in packagename?

Update:

Here is my hibernate.cfg.xml, also automatically generated by hibernate-tools (hbm2cfgxml):

<?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.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">1800</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/db</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <mapping resource="package/name/Messages.hbm.xml" />
    </session-factory>
</hibernate-configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-09-09 00:18:41

以防万一,这里是 hibernate3-maven-plugin 的自下而上方法的工作配置:

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

这是我的 src/main/database.properties 文件的内容:

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 

此设置:

  1. 生成在generate-resources期间,target/classes(带有包)中的*.hbm.xml
  2. target/classes 中生成一个 hibernate.cfg.xml,其中包含映射文件的条目。
  3. target/ generated-sources/hibernate3 中的映射生成实体(我建议遵循生成源的 target/ generated-sources/ 约定,以便它们由 IDE 自动检测)。

以下是针对包含两个表的示例数据库进行cleancompile的结果:

$ 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

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
夏见 2024-09-09 00:18:41

好吧,我明白了。我把答案放在这里< /a>.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文