栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Hibernate的Maven Java源代码生成

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Hibernate的Maven Java源代码生成

好吧,如果您不想混合使用Ant和Maven ,则可以使用Maven
Hibernate3插件
(在IMO中,这是个好主意)。它有一个

hbm2java
目标,默认情况下与该
generate-sources
阶段绑定。有关更多详细信息,请访问Mojo的网站,但该插件的设置可能类似于以下内容:

  <plugin>    <groupId>org.prehaus.mojo</groupId>    <artifactId>hibernate3-maven-plugin</artifactId>    <version>2.2</version>    <executions>      <execution>        <phase>generate-sources</phase>        <goals>          <goal>hbm2java</goal>        </goals>      </execution>    </executions>    <configuration>      <components>        <component>          <name>hbm2java</name>          <implementation>configuration</implementation>          <outputDirectory>target/generated-sources/hibernate3</outputDirectory>        </component>      </components>      <componentProperties>        <drop>true</drop>        <jdk5>true</jdk5>        <configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile>      </componentProperties>    </configuration>  </plugin>

编辑:
插件实际上

.hbm.xml
target/classes
寻找生成Java源文件。因此,如果将映射文件放在中
src/main/resources
,它们将在插件调用
target/classes
process-resources
阶段被复制到其中,并且一切正常。我刚刚使用以下示例项目对此进行了测试:

Maven的hibernate3测试用例|-pom.xml`-src    |-主要    | |-Java    | `-资源    | |-Person.hbm.xml    | `-hibernate.cfg.xml    `-测试        `-Java

pom.xml
几乎是空的,它只是包含了上面和一个JUnit依赖看到的插件配置。该
hibernate.cfg.xml
包含:

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC    "-//Hibernate/Hibernate Configuration DTD//EN"    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>  <session-factory>    <!-- Database connection settings -->    <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>    <property name="connection.url">jdbc:derby://localhost:1527/mydatabase</property>    <property name="connection.username">app</property>    <property name="connection.password">app</property>    <!-- JDBC connection pool (use the built-in) -->    <property name="connection.pool_size">1</property>    <!-- SQL dialect -->    <property name="dialect">org.hibernate.dialect.DerbyDialect</property>    <!-- Echo all executed SQL to stdout -->    <property name="show_sql">false</property>    <!-- Mapping files -->    <mapping resource="Person.hbm.xml" />  </session-factory></hibernate-configuration>

Person.hbm.xml
如下所示:

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-mapping   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>  <class name="Person" table="person">    <id name="id" type="int">      <generator  />    </id>    <property name="name" column="cname" type="string" />  </class></hibernate-mapping>

使用此配置,运行

mvn install
生成
Person.java
如下所示:

$ cat target/generated-sources/hibernate3/Person.java // default package// Generated Dec 14, 2009 2:19:22 PM by Hibernate Tools 3.2.2.GApublic class Person  implements java.io.Serializable {     private int id;     private String name;    public Person() {    }    public Person(String name) {       this.name = name;    }    public int getId() {        return this.id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return this.name;    }    public void setName(String name) {        this.name = name;    }}

一切都按说明进行。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/385355.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号