使用xdoclet开发Hibernate程序

 
  
 
使用 xdoclet 开发 J2EE
 
1). 使用 Xdoclet 生成 hbm 文件 .
    首先下载 xdoclet 的包 . 然后更改下面配置文件的 xdoclet.root.dir 文件属性
 
<?xml version="1.0" encoding="utf-8"?>
<project name= "XDoclet Examples" default= "hibernate" basedir= "." >
    <property name= "xdoclet.root.dir" value= "C:/Documents and Settings/liujs/ 桌面 /xdoclet/xdoclet-1.2.3" />
    <property name= "xdoclet.lib.dir" value= "${xdoclet.root.dir}/lib" />
    <path id= "myclasspath" >
        <fileset dir= "${xdoclet.lib.dir}" >
            <include name= "*.jar" />
        </fileset>
    </path>
     <taskdef
        name= "hibernatedoclet"
        classname= "xdoclet.modules.hibernate.HibernateDocletTask"
        classpathref= "myclasspath"
        />
    <target name= "hibernate" description= "Generate mapping documents" >
<echo> +---------------------------------------------------+ </echo>
        <echo> |                                                   | </echo>
        <echo> | R U N N I N G   H I B E R N A T E D O C L E T     | </echo>
        <echo> |                                                   | </echo>
        <echo> +---------------------------------------------------+ </echo>
<hibernatedoclet
            destdir= "./src"
            excludedtags= "@version,@author,@todo,@see"
            addedtags= "@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"
            force= "false"
            verbose= "true" >
            <fileset dir= "./src" >
                <include name= "**/*.java" />
            </fileset>
            <hibernate version= "3.0" />
        </hibernatedoclet>
    </target>
</project>
 
2). 使用 hibernate 生成从 hbm 文件生成 SQL 语句 .
< property name = "hbm2ddl.auto" > create </ property >
如果值为 create-drop 时当 session 被显示关闭后数据库中的表会被删除掉
当值为 create 时会每次都重新建立数据库
 
3)Pojo中写放xdoclet
 
package com.jianshe.model;
 
/**
   
  * @hibernate .class table = "jianshe"
  *
  * @author liujs
  */
public class User {
    private String id ;
   
    /**
      * @hibernate .id
      *    generator - class = "uuid.hex"
  *    length = 32
      */
    public String getId() {
       return id ;
    }
 
    public void setId (String id) {
       this . id = id;
    }
}
 
 
4) 常用的 xdoclet 写法
 
在类前声明表名:
 
* @hibernate .class table="app_user"
 
以下写法均在get方法前
4.1 主键的写法
/**
* @hibernate .id column="id" generator - class=" uuid.hex " unsaved - value="null"
*
*/
建议使用 generator-class="uuid.hex" 主键生成器
 
4.2 属性的写法
 
/**
* @hibernate .property length="50" not - null="true" unique="true"
 
*/
 
4.3 一对多的写法
 
private Set entries;
 
/**
* @hibernate.set
* lazy="true"
* cascade="all"
*
* @hibernate.collection-one-to-many
* class="com.xdocletbook.blog.pojo.Entry"
*
* @hibernate.collection-key
* column="blog"
*/
public Set getEntries() {
return entries;
}
 
 
如果使用了一对多,可被控方就要使用多对一,这样,插入数据的时候会执行一条SQL结约性能
private Blog blog;
/**
* @hibernate.many-to-one
*/
 
 
public Blog getBlog() {
return blog;
}
 
 
 
下面演示的是一个例子来源自<<Xdoclet in action>>演示了Blog 与 Entry 的一对多关系
 
 
 
package com.jianshe.model;
 
import java.util.Set;
 
/**
  * @hibernate .class table="Blog"
  */
public class Blog {
    private String id ;
 
    private String name ;
 
    private String owner ;
 
    private String email ;
 
    public Blog() {
    }
 
    /**
      * @hibernate .id generator - class="uuid.hex"
      */
    public String getId() {
       return id ;
    }
 
    /**
      * @hibernate .property
      */
    public String getEmail() {
       return email ;
    }
 
    /**
      * @hibernate .property
      */
    public String getName() {
       return name ;
    }
 
    /**
      * @hibernate .property
      */
    public String getOwner() {
       return owner ;
    }
 
    public void setEmail(String string) {
       email = string;
    }
 
    public void setId(String string) {
       id = string;
    }
 
    public void setName(String string) {
       name = string;
    }
 
    public void setOwner(String string) {
        owner = string;
    }
 
    private Set entries ;
 
    /**
      * @hibernate .set lazy="true" cascade="all"
      *
      * @hibernate .collection - one - to - many class="com.jianshe.model.Entry"
      *
      * @hibernate .collection - key column="blog"
      */
    public Set getEntries() {
       return entries ;
    }
 
    public void setEntries(Set set) {
       entries = set;
    }
}
 
 
 
 
 
package com.jianshe.model;
 
import java.util.Date;
 
/**
  *
  * @hibernate .class table="Entry"
  */
public class Entry {
    private String id ;
 
    private String text ;
 
    private String title ;
 
    private Date createdDate ;
 
    public Entry() {
    }
 
    private Blog blog ;
 
    /**
      * @hibernate .many - to - one
      */
    public Blog getBlog() {
       return blog ;
    }
 
    public void setBlog(Blog blog) {
       this . blog = blog;
    }
 
    /**
      * @hibernate .id generator - class="uuid.hex"
      */
    public String getId() {
       return id ;
    }
 
    /**
      * @hibernate .property
      */
    public String getText() {
       return text ;
    }
 
    /**
      * @hibernate .property
      */
    public String getTitle() {
       return title ;
    }
 
    /**
      * @hibernate .property
      */
    public Date getCreatedDate() {
       return createdDate ;
    }
 
    public void setId(String string) {
       id = string;
    }
 
    public void setText(String string) {
       text = string;
    }
 
    public void setTitle(String string) {
       title = string;
 
    }
 
    public void setCreatedDate(Date date) {
       createdDate = date;
    }
}
 
 
录入完这两个类后 , 你使用 xdoclet 来生成
 
Hbm 文件 . 当你指定了 hibernate 的属性
 
< property name = "hbm2ddl.auto" > create </ property >
数据库就会自动生成了 . 不过它每次都生帮你先删除表再重建的 . 所以应用启动时一定要去掉个这属性的设置 .
 
 
关于使用代码提示的问题需要在 eclipse 中装一个叫做 doclipse 的插件 , 兄弟 . 别看了开始动手尝试一下吧 .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值