☆蓝色梦想☆

世界总是反反覆覆错错落落地飘去 来不及叹息 生活不是平平淡淡从从容容的东西 不能放弃
posts - 57, comments - 5, trackbacks - 0, articles - 0

XDoclet(ant)生成Hibernate映射文件

Posted on 2006-08-18 16:36 ☆蓝色梦想☆ 阅读(423) 评论(0)  编辑  收藏 所属分类: Hibernate
java--->XDoclet(hibernatedoclet)--->Hbm---->SchemaExport(schemaexport,hbm2ddl)---->数据表

 1/**/ /* 
 2 * Created on 2006-4-7
 3  */
 
 4 
 5 package  com.entity;
 6
 7 /** */ /** 
 8 *  @author  jkallen
 9 * @hibernate.class lazy="true" table="syn_dept"
10 * @hibernate.cache usage="read-write"
11  */
 
12  public   class  SynDepartment   {
13 
14  /** */ /**  主键 id */ 
15  private  Long id;
16  /** */ /**  部门名称 */ 
17  private  String code_name;
18 
19  /** */ /** 
20  *  @return  Returns the id.
21  * @hibernate.id generator-class="native" column="id"
22   */
 
23      public  Long getId()   {
24   return  id;
25 }
 
26   public   void  setId(Long id)   {
27   this .id  =  id;
28 }
 
29   /** */ /** 
30    *  @return  Returns the code_name.
31    * @hibernate.property column = "code_name"
32     */
 
33   public  String getCode_name()   {
34   return  code_name;
35 }
 
36   public   void  setCode_name(String code_name)   {
37   this .code_name  =  code_name;
38 }
 
39}
 
40
这里用到了几种@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" />
   
</fileset>
   
<hibernate version="2.0" />
  
</hibernatedoclet>

  
<copy todir="${classes.dir}">
   
<fileset dir="${generated.dir}" />
  
</copy>
 
</target>


通过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
="&#xd;&#xa;go&#xd;&#xa;" output="${sql.dir}/${synup.sql.file}"
   
>
   
<fileset refid="hibernate.synup.mapping.files" />
  
</schemaexport>
  
<echo message="Output sql to file: ${sql.dir}/${sql.file}" />
 
</target>
 
<fileset id="hibernate.synup.mapping.files" dir="${classes.dir}">
  
<include name="**/entity/*.hbm.xml" />
 
</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,最后生成的映射文件如下:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//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">
            
</generator>
        
</id>

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

        
<!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-SynDepartment.xml
            containing the additional properties and place it in your merge dir.
        
-->

    
</class>

</hibernate-mapping>


 控制台中部分信息如下:

[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表了,快去看下吧!

关于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


只有注册用户登录后才能发表评论。


网站导航: