使用映射文件定制持久化类

 
hbm2java工具探讨
作用:根据映射文件自动生成java源文件
要想使用该工具,我们首先需要创建对象-关系映射文件。
示例:Customer.hbm.xml
<hibernate-mapping>
         <class name=”mypack.Customer” table=”CUSTOMERS”>
                   <meta attribute=”class-description”>
                            Represents a single customer.
                            @author LindaSun
                  </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 customer was Registered</meta>
                            <meta attribute=”use-in-tostring”>true</meta>
                            <column name=”REGISTERED_TIME” index=”IDX_REGISTERED_TIME”
sql-type=”timestample” />
                  </property>
 
                  <property name=”age” type=”int”>
                            <meta attribute=”field-description”>How old is the customer</meta>
                            <meta attribute=”use-in-tostring”>true</meta>
                            <column name=”AGE” check=”AGE>10” not-null=”true” />
                  </property>
 
                  <property name=”married” type=”boolean” column=”IS_MARRIED”>
                            <meta attribute=”field-description”>Is the customer 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>
 
I.         定制持久化类
<meta>元素用于精粒度的控制Java源代码的内容。下面是其主要用法:
(1)       指定描述类的JavaDoc,如下:
<class name=”mypack.Customer” >
         <meta attribute=”class-description”>
                  Represents a single customer.
                  @author LindaSun
         </meta>
         …
</class>
其可用hbm2java工具生成的Java源代码如下:
         /**
         * Represents a single customer.
         * @author LindaSun
         *
         */
         public class Customer implements Serializable
(2)       指定类所继承的类,如下:
<class name=”mypack.Customer”>
          <meta attribute=”extends”>mypack.BusinessObject</meta>
        …
</class>
其表示Customer类继承了mypack.BusinessObject类,hbm2java工具生成源代码如下:
public class Customer extends mypack.BusinessObject implements Serializable
(3)       指定描述类的属性的JavaDoc,如下:
<property name=”married” type=”boolean” column=”IS_MARRIED”>
          <meta attribute=”field-description”>Is the customer married</meta>
        …
</property>
生成如下源代码:
          /**
        * Is the customer married
        */
          publicc Boolean getMarried(){
                 return this.married;
        }
(4)       指定类、类的属性及类属性的getXXX()方法或setXXX()方法的修饰符,可选值包括:staticfinalabstractpublicprotectedprivate。如下:
<id name=”id” type=”int”>
          <meta attribute=”scope-set”>protected</meta>
          <generator class=”native” />
</id>
表示setId()方法为protected类型,hbm2java工具生成的Java源代码如下:
protected setId(Integer id){
          this.id=id;
}
PS
:在用hbm2java工具生成Java源代码时,不会检查设置的修饰符是否正确。
以下代码表明Customer类为public类型
<class name=”mypack.Customer” table=”CUSTOMERS”>
        <meta attribute=”class-scope”>public</meta>
        …
</class>
(5)       指定在类的toString()方法返回的字符串中是否包含特定的属性,如下代码:
<property name=”name” type=”string” not-null=”true”>
          <meta attribute=”use-in-tostring”>true</meta>
</property>
表示在toString()方法返回的字符串中包含name属性,hbm2java工具生成的java源代码如下:
          public String toString(){
                 return new ToStringBuilder(this)
                          .append(“id”,getId())
                          .append(“name”,getName())
                          .append(“registeredTime”,getRegisteredTime())
                          .append(“age”,getAge())
                          .append(“married”,getMarried())
                          .append(“description”,getDescription())
                          .toString();
        }
 
 
下表中列出了<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
指定类的属性的修饰符,默认为private
use-in-toString
如果为true,表示在toString方法中包含此属性
gen-property
如果是false,不会在Java类中生成此属性,默认为true
finder-method
指定find方法名,参见以下文章
 
 
PS<meta>元素是有作用范围的,如果在映射文件的开头声明了如下<meta>元素:
<hibernate-mapping>
         <meta attribute=”extends”>mypack.BusinessObject</meta>
         <class name=”mypack.Class1”>…</class>
         <class name=”mypack.Class2”>…</class>
         …
</hibernate-mapping>
映射文件中所有的类都继承mypack.BusinessObject类。
若只想将紧靠<meta>元素的Class1类扩展其类,一种办法如下:设置<meta>元素的inherit设为false
         <hibernate-mapping>
                  <meta attribute=”extends” inherit=”false”>mypack.BusinessObject</meta>
                  <class name=”mypack.Class1”>…</class>
                  <class name=”mypack.Class2”>…</class>
                  ….
         </hibernate-mapping>
另一种办法是在其class元素内部定义extends属性:
         <hibernate-mapping>
                  <class name=”mypack.Class1”>
                            <meta attribute=”extends”>mypack.BusinessObject</meta>
                            …
                  </class>
                  <class name=”mypack.Class2”>…</class>
         </hibernate-mapping>
 
再看下例:
<hibernate-mapping>
         <class name=”mypack.Class1”>
                  <meta attribute=”extends”>mypack.BusinessObject</meta>
                  <meta attribute=”scope-field”>protected</meta>
                  <property name=”field1” type=”string” />
                  <property name=”field2” type=”string” />
                  <property name=”field3” type=”string” >
                  <meta attribute=”scope-field”>public</meta>
         </property>
         </class>
</hibernate-mapping>
结果在Class1定义了三个属性:field1field2field3,它们的修饰符如下:
         protected String field1;
         protected String field2;
         public String field3; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值