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

来源:百度文库 编辑:神马文学网 时间:2024/04/29 19:53:08
0
推荐
通过XDoclet可以我们的精力放在编写java源文件上。
具体来说就是:
只有Java: java--->XDoclet(hibernatedoclet)--->Hbm---->SchemaExport(schemaexport,hbm2ddl)---->数据表
1:java源文件编写
/**/ /*
 * Created on 2006-4-7
  */

package  com.entity;

/** */ /**
 *  @author  jkallen
 * @hibernate.class lazy="true" table="syn_dept"
 * @hibernate.cache usage="read-write"
  */
public   class  SynDepartment {

  /** */ /**  主键 id */
  private  Long id;
  /** */ /**  部门名称 */
  private  String code_name;

  /** */ /**
  *  @return  Returns the id.
  * @hibernate.id generator-class="native" column="id"
   */
     public  Long getId() {
   return  id;
 }
  public   void  setId(Long id) {
   this .id  =  id;
 }
  /** */ /**
    *  @return  Returns the code_name.
    * @hibernate.property column = "code_name"
     */
  public  String getCode_name() {
   return  code_name;
 }
  public   void  setCode_name(String code_name) {
   this .code_name  =  code_name;
 }
}


这里用到了几种@hibernate标记的用法
@hibernate.class标记指定类的映射代码,lazy="true" table="syn_dept"则如
hibernate的映射文件class元素的属性值具有相同的意义
@hibernate.id标记指定类的OID映射代码
@hibernate.property标记指定类的属性映射代码
另外还可能用到@hibernate.set(如一对多的情况下)
2:XDoclet--->Hbm(写在build.xml文件中,ANT运行)
< target  name ="toHbm"
  depends ="compileEntity"
  description ="Generate hibernate mapping documents" >
   < hibernatedoclet  destdir ="${generated.dir}" >
    < fileset  dir ="${src.dir}" >
     < include  name ="**/entity/*.java"   />
   
    < hibernate  version ="2.0"   />
  

   < copy  todir ="${classes.dir}" >
    < fileset  dir ="${generated.dir}"   />
  
 
通过hibernatedoclet就可以生成SynDepartment.hbm.xml映射文件
fileset顾名思义就是过滤文件了。
注:compileEntity--编译java源文件(自定义)
3:SchemaExport---->数据表
< target  name ="toddl"  depends ="init" >
   < schemaexport  properties ="${classes.dir}/hibernate.properties"
   quiet ="no"  text ="no"  drop ="no"
   delimiter =" go "  output ="${sql.dir}/${synup.sql.file}"
    >
    < fileset  refid ="hibernate.synup.mapping.files"   />
  
   < echo  message ="Output sql to file: ${sql.dir}/${sql.file}"   />
 
  < fileset  id ="hibernate.synup.mapping.files"  dir ="${classes.dir}" >
   < include  name ="**/entity/*.hbm.xml"   />
 

通过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中执行:


OK,最后生成的映射文件如下:


    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

< hibernate-mapping >
     < class
         name ="com.SynDepartment"
        table ="syn_dept"
        dynamic-update ="false"
        dynamic-insert ="false"
     >
         < cache  usage ="read-write"   />

         < id
             name ="id"
            column ="id"
            type ="java.lang.Long"
         >
             < generator  class ="native" >
            
        

         < property
             name ="code_name"
            type ="java.lang.String"
            update ="true"
            insert ="true"
            access ="property"
            column ="code_name"
         />

        

    



控制台中部分信息如下:
[schemaexport] drop table syn_dept cascade constraints
[schemaexport] go
[schemaexport] drop sequence hibernate_sequence
[schemaexport] go
[schemaexport] create table syn_dept (
[schemaexport] id number(19,0) not null,
[schemaexport] code_name varchar2(255),
[schemaexport] primary key (id)
[schemaexport] )
DB中已经生成syn_dept表了,快去看下吧!