`

通过XDoclet(ant)生成Hibernate映射文件

阅读更多

通过XDoclet可以我们的精力放在编写java源文件上。

具体来说就是:
只有Java: java--->XDoclet(hibernatedoclet)--->Hbm---->SchemaExport(schemaexport,hbm2ddl)---->数据表

1:java源文件编写

420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> /**/ /*
420){this.resized=true;this.style.width=420;}" align=top> * Created on 2006-4-7
420){this.resized=true;this.style.width=420;}" align=top> 
*/

420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>
package  com.entity;
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>420){this.resized=true;this.style.width=420;}" align=top>
/** */ /**
420){this.resized=true;this.style.width=420;}" align=top> * 
@author  jkallen
420){this.resized=true;this.style.width=420;}" align=top> * @hibernate.class lazy="true" table="syn_dept"
420){this.resized=true;this.style.width=420;}" align=top> * @hibernate.cache usage="read-write"
420){this.resized=true;this.style.width=420;}" align=top> 
*/

420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top>
public   class  SynDepartment  420){this.resized=true;this.style.width=420;}"> {
420){this.resized=true;this.style.width=420;}" align=top> 
420){this.resized=true;this.style.width=420;}" align=top>420){this.resized=true;this.style.width=420;}" align=top> 
/** */ /**  主键 id */
420){this.resized=true;this.style.width=420;}" align=top> 
private  Long id;
420){this.resized=true;this.style.width=420;}" align=top>420){this.resized=true;this.style.width=420;}" align=top> 
/** */ /**  部门名称 */
420){this.resized=true;this.style.width=420;}" align=top> 
private  String code_name;
420){this.resized=true;this.style.width=420;}" align=top> 
420){this.resized=true;this.style.width=420;}" align=top>420){this.resized=true;this.style.width=420;}" align=top> 
/** */ /**
420){this.resized=true;this.style.width=420;}" align=top>  * 
@return  Returns the id.
420){this.resized=true;this.style.width=420;}" align=top>  * @hibernate.id generator-class="native" column="id"
420){this.resized=true;this.style.width=420;}" align=top>  
*/

420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top>    
public  Long getId()  420){this.resized=true;this.style.width=420;}"> {
420){this.resized=true;this.style.width=420;}" align=top>  
return  id;
420){this.resized=true;this.style.width=420;}" align=top> }

420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> 
public   void  setId(Long id)  420){this.resized=true;this.style.width=420;}"> {
420){this.resized=true;this.style.width=420;}" align=top>  
this .id  =  id;
420){this.resized=true;this.style.width=420;}" align=top> }

420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> 
/** */ /**
420){this.resized=true;this.style.width=420;}" align=top>    * 
@return  Returns the code_name.
420){this.resized=true;this.style.width=420;}" align=top>    * @hibernate.property column = "code_name"
420){this.resized=true;this.style.width=420;}" align=top>    
*/

420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> 
public  String getCode_name()  420){this.resized=true;this.style.width=420;}"> {
420){this.resized=true;this.style.width=420;}" align=top>  
return  code_name;
420){this.resized=true;this.style.width=420;}" align=top> }

420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> 
public   void  setCode_name(String code_name)  420){this.resized=true;this.style.width=420;}"> {
420){this.resized=true;this.style.width=420;}" align=top>  
this .code_name  =  code_name;
420){this.resized=true;this.style.width=420;}" align=top> }

420){this.resized=true;this.style.width=420;}" align=top>}

420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>



这里用到了几种@hibernate标记的用法
@hibernate.class标记指定类的映射代码,lazy="true" table="syn_dept"则如
hibernate的映射文件class元素的属性值具有相同的意义
@hibernate.id标记指定类的OID映射代码
@hibernate.property标记指定类的属性映射代码
另外还可能用到@hibernate.set(如一对多的情况下)

2:XDoclet--->Hbm(写在build.xml文件中,ANT运行)

420){this.resized=true;this.style.width=420;}" align=top> < target  name ="toHbm"  
420){this.resized=true;this.style.width=420;}" align=top>  depends
="compileEntity"  
420){this.resized=true;this.style.width=420;}" align=top>  description
="Generate hibernate mapping documents" >
420){this.resized=true;this.style.width=420;}" align=top>  
< hibernatedoclet  destdir ="${generated.dir}" >
420){this.resized=true;this.style.width=420;}" align=top>   
< fileset  dir ="${src.dir}" >
420){this.resized=true;this.style.width=420;}" align=top>    
< include  name ="**/entity/*.java"   />
420){this.resized=true;this.style.width=420;}" align=top>   
</ fileset >
420){this.resized=true;this.style.width=420;}" align=top>   
< hibernate  version ="2.0"   />
420){this.resized=true;this.style.width=420;}" align=top>  
</ hibernatedoclet >
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>  
< copy  todir ="${classes.dir}" >
420){this.resized=true;this.style.width=420;}" align=top>   
< fileset  dir ="${generated.dir}"   />
420){this.resized=true;this.style.width=420;}" align=top>  
</ copy >
420){this.resized=true;this.style.width=420;}" align=top> 
</ target >


通过hibernatedoclet就可以生成SynDepartment.hbm.xml映射文件
fileset顾名思义就是过滤文件了。
注:compileEntity--编译java源文件(自定义)

3:SchemaExport---->数据表

420){this.resized=true;this.style.width=420;}" align=top> < target  name ="toddl"  depends ="init" >
420){this.resized=true;this.style.width=420;}" align=top>  
< schemaexport  properties ="${classes.dir}/hibernate.properties"  
420){this.resized=true;this.style.width=420;}" align=top>   quiet
="no"  text ="no"  drop ="no"     
420){this.resized=true;this.style.width=420;}" align=top>   delimiter
="&#xd;&#xa;go&#xd;&#xa;"  output ="${sql.dir}/${synup.sql.file}"
420){this.resized=true;this.style.width=420;}" align=top>   
>
420){this.resized=true;this.style.width=420;}" align=top>   
< fileset  refid ="hibernate.synup.mapping.files"   />
420){this.resized=true;this.style.width=420;}" align=top>  
</ schemaexport >
420){this.resized=true;this.style.width=420;}" align=top>  
< echo  message ="Output sql to file: ${sql.dir}/${sql.file}"   />
420){this.resized=true;this.style.width=420;}" align=top> 
</ target >
420){this.resized=true;this.style.width=420;}" align=top> 
< fileset  id ="hibernate.synup.mapping.files"  dir ="${classes.dir}" >
420){this.resized=true;this.style.width=420;}" align=top>  
< include  name ="**/entity/*.hbm.xml"   />
420){this.resized=true;this.style.width=420;}" align=top> 
</ fileset >
通过schemaexport就向DB中生成table了。其中可能用到如下的一些属性:
 quiet:如果为yes,表示不把子DDL脚本输出到控制台
 drop:如果为yes,只执行删除数据库中的操作,但不创建新的表
 text:如果为yes,只会生成DDL脚本文件,但不会在数据库中执行DDL脚本
 output:指定存放DDL脚本文件的目录
 config:设定基于XML格式的配置文件, hbm2ddl(schemaexport)工具从这个文件中读取数据库的配置信息
 properties:设定基于java属性文件格式的配置文件,hbm2ddl(schemaexport)工具从这个文件中读取DB的配置信息
 format:设定DDL脚本中SQL语句的格式
 delimiter:为DDL脚本设置行结束符
 
 在ANT中执行:
 <target name="initOnlySynup" depends="toHbm,toddl">
 </target>
 
 OK,最后生成的映射文件如下:
420){this.resized=true;this.style.width=420;}" align=top> <? xml version="1.0" ?>
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>
<! DOCTYPE hibernate-mapping PUBLIC
420){this.resized=true;this.style.width=420;}" align=top>    "-//Hibernate/Hibernate Mapping DTD 2.0//EN" 
420){this.resized=true;this.style.width=420;}" align=top>    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>
< hibernate-mapping >
420){this.resized=true;this.style.width=420;}" align=top>    
< class
420){this.resized=true;this.style.width=420;}" align=top>        
name ="com.SynDepartment"
420){this.resized=true;this.style.width=420;}" align=top>        table
="syn_dept"
420){this.resized=true;this.style.width=420;}" align=top>        dynamic-update
="false"
420){this.resized=true;this.style.width=420;}" align=top>        dynamic-insert
="false"
420){this.resized=true;this.style.width=420;}" align=top>    
>
420){this.resized=true;this.style.width=420;}" align=top>        
< cache  usage ="read-write"   />
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>        
< id
420){this.resized=true;this.style.width=420;}" align=top>            
name ="id"
420){this.resized=true;this.style.width=420;}" align=top>            column
="id"
420){this.resized=true;this.style.width=420;}" align=top>            type
="java.lang.Long"
420){this.resized=true;this.style.width=420;}" align=top>        
>
420){this.resized=true;this.style.width=420;}" align=top>            
< generator  class ="native" >
420){this.resized=true;this.style.width=420;}" align=top>            
</ generator >
420){this.resized=true;this.style.width=420;}" align=top>        
</ id >
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>        
< property
420){this.resized=true;this.style.width=420;}" align=top>            
name ="code_name"
420){this.resized=true;this.style.width=420;}" align=top>            type
="java.lang.String"
420){this.resized=true;this.style.width=420;}" align=top>            update
="true"
420){this.resized=true;this.style.width=420;}" align=top>            insert
="true"
420){this.resized=true;this.style.width=420;}" align=top>            access
="property"
420){this.resized=true;this.style.width=420;}" align=top>            column
="code_name"
420){this.resized=true;this.style.width=420;}" align=top>        
/>
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>        
<!--
420){this.resized=true;this.style.width=420;}" align=top>            To add non XDoclet property mappings, create a file named
420){this.resized=true;this.style.width=420;}" align=top>                hibernate-properties-SynDepartment.xml
420){this.resized=true;this.style.width=420;}" align=top>            containing the additional properties and place it in your merge dir.
420){this.resized=true;this.style.width=420;}" align=top>        
-->
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>    
</ class >
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>
</ hibernate-mapping >
420){this.resized=true;this.style.width=420;}" align=top>


 控制台中部分信息如下:

420){this.resized=true;this.style.width=420;}" align=top> [schemaexport] drop table syn_dept cascade constraints
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] go
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] drop sequence hibernate_sequence
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] go
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] create table syn_dept (
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] id number(19,0) not null,
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] code_name varchar2(255),
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] primary key (id)
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] )

DB中已经生成syn_dept表了,快去看下吧!

关于Xdoclet 中的hibernate标签更多信息可以参考:
http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html#@hibernate_collection-key__0__1_
我还在一个网友的博客上看到了他对此的汉化:
http://blog.csdn.net/fasttalk/archive/2005/09/19/484615.aspx


参考资料:Hibernate持久化技术详解

来源:http://www.blogjava.net/Jkallen/archive/2006/04/10/40305.html

分享到:
评论

相关推荐

    ant hibernate mapping 生成映射文件 生成配置文件

    &lt;property name="xdoclet.home" value="D:/jarpackage/xdoclet-plugins-1.0.3"/&gt; 指出这个文件存放的位置.然后就可以使用了...里面有一个成功的例子 请读者参考 在构建域对象的时候一定要指明在配置文件中的配置信息...

    新建 Micro用ant执行POJO自动生成映射文件及数据表soft Word 文档

    关于用hibernate映射类时涉及到一个问题: 1.先创建数据表再写JOPO类和映射文件 2.先写POJO类,然后映射文件,最后数据库 3.先映射文件,再同时生成POJO类和数据库 第1种 方法有背面向对象原理,而且控制起来不大...

    最新版ssh2框架整合

    使用最新的ssh搭建起来的框架!希望对初学者有所帮助 hibernate映射文件,配置文件使用的是ant、xdoclet生成的!

    Hibernate 中文 html 帮助文档

    1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...

    Hibernate+中文文档

    1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...

    hibernate3.2中文文档(chm格式)

    1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...

    HibernateAPI中文版.chm

    1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...

    hibernate 教程

    映射文件生成器(Mapping File Generation) 15.3.1. 运行此工具 16. 示例:父子关系(Parent Child Relationships) 16.1. 关于collections 16.2. 双向的一对多关系(Bidirectional one-to-many) 16.3...

    Hibernate中文详细学习文档

    1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...

    hibernate 体系结构与配置 参考文档(html)

    映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...

    Hibernate教程

    2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二部分 - 关联映射 2.3.1. 映射Person类 2.3.2. 一个单向的Set-based关联 2.3.3. 使关联工作 ...

    最全Hibernate 参考文档

    22.2. Hibernate 映射 22.3. Hibernate 代码 23. 示例:复杂映射实例 23.1. Employer(雇主)/Employee(雇员) 23.2. Author(作家)/Work(作品) 23.3. Customer(客户)/Order(订单)/Product(产品) 23.4. 杂例 23.4.1. ...

    hibernate

    映射文件生成器(Mapping File Generation) 15.3.1. 运行此工具 16. 示例:父子关系(Parent Child Relationships) 16.1. 关于collections 16.2. 双向的一对多关系(Bidirectional one-to-many) 16.3...

    hibernate3.04中文文档.chm

    2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二部分 - 关联映射 2.3.1. 映射Person类 2.3.2. 一个单向的Set-based关联 2.3.3. 使关联...

    hibernate 框架详解

    映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二部分 - 关联映射 2.3.1. 映射Person类 2.3.2. 一个单向的Set-based关联 2.3.3. 使关联工作 2.3.4...

    Hibernate参考文档

    1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...

    Hibernate3的帮助文档

    2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二部分 - 关联映射 2.3.1. 映射Person类 2.3.2. 一个单向的Set-based关联 2.3.3. 使关联工作 ...

    Hibernate3+中文参考文档

    22.2. Hibernate 映射 22.3. Hibernate 代码 23. 示例:复杂映射实例 23.1. Employer(雇主)/Employee(雇员) 23.2. Author(作家)/Work(作品) 23.3. Customer(客户)/Order(订单)/Product(产品) 23.4. 杂例 23.4.1. ...

Global site tag (gtag.js) - Google Analytics