如何配置 hbm2java 和 hbm2dao 将包名添加到生成的类中
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以防万一,这里是 hibernate3-maven-plugin 的自下而上方法的工作配置:
这是我的 src/main/database.properties 文件的内容:
此设置:
generate-resources
期间,target/classes
(带有包)中的*.hbm.xml
。target/classes
中生成一个hibernate.cfg.xml
,其中包含映射文件的条目。target/ generated-sources/hibernate3
中的映射生成实体(我建议遵循生成源的target/ generated-sources/
约定,以便它们由 IDE 自动检测)。以下是针对包含两个表的示例数据库进行
cleancompile
的结果:Just in case, here is a working configuration of the hibernate3-maven-plugin for a bottom-up approach:
And here is the content of my
src/main/database.properties
file:This setup:
*.hbm.xml
intarget/classes
(with the package) duringgenerate-resources
.hibernate.cfg.xml
intarget/classes
with entries for the mapping files.target/generated-sources/hibernate3
(I recommend following thetarget/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:好吧,我明白了。我把答案放在这里< /a>.
Ok, I figured it out. I put the answer here.