posts - 70,comments - 408,trackbacks - 0
      Hibernate为了简化开发,给我们提供了四种工具,hbm2java,hbm2ddl,XDoclet,Middlegen.其中我觉得hbm2java(根据映射文件生成JAVA源文件),hbm2ddl(根据映射文件生成数据库文件,这两个工具非常实用.还有XDoclet(根据带有XDoclet标记的JAVA源文件生成映射文件).Middlegen(根据数据库文件生成映射文件的工具也不错.不过我没怎么使用过.所以这里就不多说了.好了下面我们创建一个对象-关系映射文件,来演示一下这两种工具的使用方法.首先创建我们的XML映射文件.这里命名为Demo.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
 <class name="hibernate.Demo" table="DEMOS">
  <meta attribute="class-description">ToolDemo</meta>
  <meta attribute="class-scope">public</meta>
  
  <id name="id" type="long" column="ID">
   <meta attribute="scope-set">protected</meta>
   <generator class="native"/>
  </id>
  
  <property name="name" type="string">
   <meta attribute="finder-method">findByName</meta>
   <meta attribute="use-in-tostring">true</meta>
   <column name="NAME" length="15" not-null="true" unique="true"/>
  </property>
  
  <property name="registeredTime" type="timestamp">
   <meta attribute="field-description">When the Demo</meta>
   <meta attribute="use-in-tostring">true</meta>
   <column name="REGISTERED_TIME" index="IDX_REGISTERED_TIME" sql-type="timestamp"/>
  </property>
  
  <property name="age" type="int">
   <meta attribute="field-description">How old is the Demo</meta>
   <meta attribute="use-in-tostring">true</meta>
   <column name="AGE" check="AGE>10" not-null="true"/>
  </property>
  
  <property name="sex" column="SEX" type="char"></property>
  
  <property name="married" type="boolean" column="IS_MARRIED">
   <meta attribute="field-description">Is the Demo married</meta>
   <meta attribute="use-in-tostring">true</meta>
  </property>
  
  <property name="description" type="string">
   <meta attribute="use-in-tostring">true</meta>
   <column name="DESCRIPTION" sql-type="text"/> 
  </property>
 </class>
</hibernate-mapping>

      解释下上面的XML文件,这里主要说明是的<meta>元素,用于精确的控制JAVA源文件的内容.
  <meta attribute="class-description">ToolDemo</meta>这里是源文件类的注释,用于生成javadoc
  <meta attribute="class-scope">public</meta>这个是类的修饰符
  <meta attribute="extends">hibernate.Tool</meta>这里声明这个类继承于哪个类,这里是继承hibernate包下的Tool类
   <meta attribute="field-description">Is the Demo married</meta>这个也是注释,是属性的注释,用于生成javadoc
   <meta attribute="use-in-tostring">true</meta>这个是指定在类的toString()方法返回的字符串是否包含这个属性.
   <meta attribute="scope-set">protected</meta>指定类的属性的get或set方法的修饰符,包括static public final privete等等
   
   下面列出<meta>元素的所有属性的用法
   class-description指定类的注释,javadoc
   field-description指定类的属性的注释,javadoc
   interface如果是true表示生成的就接口,不是类,默认是false
   implements指定类要实现的接口
   extends指定类要继承的类  
   generated-class重新生成类名
   scope-class指定类的修饰复,默认的是public
   scope-set设置set方法的修饰复,默认public
   scope-get设置get方法的修饰复,默认public
   scope-field设定类的属性的修饰复,默认public
   use-in-tostring如果是true表示在类的toString()方法中包含这个属性
   gen-property如果是false表示不会在类中生成这个属性,默认是true
   finder-method设定find方法名

   下面列出<column>元素的所有属性和用法
   name设置表字段的名字
   length设置表字段的长度
   not-null如果是true说明字段不可以为null,默认是false
   unique如果是true,设置字段唯一性约束,默认是false
   index给一个字段或者多个字段建立索引
   unique-key为多个字段设置唯一约束
   freign-key为外键约束命名
   sql-type设定字段的SQL类型
   check设定SQL检查约束

      基础知识就以上这些,记住就OK了hbm2ddl位于Hibernate软件包中,而hbm2java工具位于Hibernate的扩展包中,真不理解Hibernate为什么这么搞,弄的好是麻烦,一起用的两个工具要分开放在两个包里哎,好了废话不说,在www.hibernate.org上可以下载到单独的Hibernate扩展包,hibernate-tools.jar放到我们的classpath下,然后创建build.xml用ant来运行这两个工具就OK了.非常方便.有时间在把ANT的详细使用方法发布到我的BLOG上.
posted on 2007-05-17 16:47 我心依旧 阅读(5060) 评论(0)  编辑  收藏

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


网站导航: