如何配置hbm2java和hbm2dao以将包名添加到生成的类 [英] How to configure hbm2java and hbm2dao to add packagename to generated classes

查看:100
本文介绍了如何配置hbm2java和hbm2dao以将包名添加到生成的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用maven配置 hbm2java 来生成POJO类和DAO对象。我正在处理的一个问题是软件包名称不会生成。我为此使用了以下pom:

 < execution> 
< id> hbm2java< / id>
<阶段> generate-sources< / phase>
<目标>
< goal> hbm2java< / goal>
< /目标>
<继承>假< /继承>
<配置>
<组件>
< component>
<名称> hbm2java< /名称>
< implementation>配置< / implementation>
< / component>
< /组件>
< componentProperties>
<包裹名称>包裹名称< /包裹名称>
< configurationfile> target / hibernate3 / generated-mappings / hibernate.cfg.xml< / configurationfile>
< / componentProperties>
< / configuration>
< /执行>

然而,生成的代码从以下开始:

  //默认包
//生成2010-05-17 13:11:51 by Hibernate Tools 3.2.2.GA

/ **
*由hbm2java生成的消息
* /
public class消息实现java.io.Serializable {

有没有办法迫使maven生成 packagename 中定义的部分?



更新:

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

 <?xml version =1.0encoding =utf-8?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置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>密码< / property>
< property name =hibernate.connection.url> jdbc:postgresql:// localhost:5432 / db< / property>
< property name =hibernate.connection.username>用户名< / property>
< property name =hibernate.dialect> org.hibernate.dialect.PostgreSQLDialect< / property>
< mapping resource =package / name / Messages.hbm.xml/>
< / session-factory>
< / hibernate-configuration>


解决方案

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

 < plugin> 
< groupId> org.codehaus.mojo< / groupId>
< artifactId> hibernate3-maven-plugin< / artifactId>
< version> 2.2< / version>
<执行次数>
<执行>
< id> generate-xml-files< / id>
<阶段>生成资源< /阶段>
<目标>
< goal> hbm2hbmxml< / goal>
< goal> hbm2cfgxml< / goal>
< /目标>
< /执行>
<执行>
< id>生成实体< / id>
<阶段> generate-sources< / phase>
<目标>
< goal> hbm2java< / goal>
< /目标>
< /执行>
< /执行次数>
<配置>
<组件>
< component>
<名称> hbm2hbmxml< /名称>
< implementation> jdbc配置< / implementation>
< outputDirectory> target / classes< / outputDirectory>
< / component>
< component>
<名称> hbm2cfgxml< / name>
< implementation> jdbc配置< / implementation>
< outputDirectory> target / classes< / outputDirectory>
< / component>
< component>
<名称> hbm2java< /名称>
< implementation>配置< / implementation>
< outputDirectory> target / generated-sources / hibernate3< / outputDirectory>
< / component>
< /组件>
< componentProperties>
< propertyfile> src / main / resources / database.properties< / propertyfile>
< jdk5> true< / jdk5>
< ejb3> false< / ejb3>
< packagename> com.mycompany.myapp< /包裹名称>
< format> true< / format>
< haltonerror> true< / haltonerror>
< / componentProperties>
< / configuration>
<依赖关系>
< dependency>
< groupId> org.apache.derby< / groupId>
< artifactId> derby< / artifactId>
< version> 10.5.3.0_1< / version>
< /依赖关系>
< /依赖关系>
< / plugin>

这里是我的 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。 diabyDialectDialect

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

此设置:


  1. 生成 generate-resources 目标/类(带包)中的code> *。hbm.xml / code>。

  2. target / classes 中生成 hibernate.cfg.xml code>,其中包含映射文件的条目。
  3. target / generated-sources / hibernate3 中的映射生成实体。我建议遵循 target / generated-sources /< tool> 约定,以便它们可以被IDE自动检测到)

以下是针对具有两个表的示例数据库的 clean compile 的结果:

 
$ mvn clean编译
...
$树目标/
目标/
├──类
│├──com
││└──mycompany
││└──myapp
││├──Department.class
││├──Department.hbm.xml
│ │├──Employee.class
││└──Employee.hbm.xml
│├──database.properties
│└─hibernate.cfg.xml
└ ──生成源
└──hibernate3
└──com
└──mycompany
└──myapp
├──Department.java
└──Employee.java


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>

解决方案

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

这篇关于如何配置hbm2java和hbm2dao以将包名添加到生成的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆