XDoclet in Action 下載地址: http://www.infoxa.com/asp/book/xxnr.asp?id=1570
XDoclet實現基本原理是,通過在Java代碼加入特定的JavaDoc tag,從而爲其添加特定
的附加語義,之後通過XDoclet工具對代碼中JavaDoc Tag進行分析,自動生成與代碼對應
的配置文件,XDoclet。
血的教訓,不要用 Key 當作變量名,不然無法自動生成數據庫表。
table | 類對應的表名,默認值:當前類名 |
where | 數據甄選條件,如果只需要處理庫表中某些特定數據的時候,可通過此選項設定結果集限定條件。 |
dynamic-update |
生成Update SQL時,僅包含發生變動的字段,默認值: false。dynamic-update="true"時,Update SQL 時候,只包括當前發生變化的字段(提高DB Update性能)。 |
dynamic-insert |
生成Insert SQL時,僅包含非空(null)字段,默認值:false。 dynamic-insert="true" 時,Insert SQL 時候,只包括當前非空字段。(提高DB Insert性能) |
discriminator-value | 子類辨別標識,用於多態支持。 discriminator-value="1" discriminator-value 參數的目的是對多態提供支持。請參見下面關於@hibernate.discriminator的說明。 |
Proxy | 代理類,默認值:空。 proxy="" 表明當前類不使用代理(Proxy)。代理類的作用是爲Lazy.Loading提供支持 |
lazy | Specifies the class itself to use for CGLIB proxy interface 默認:false lazy ="false"表示不採用延遲加載 |
@hibernate.discriminator(識別器) 用於提供多態支持。
column | 用於區分各子類的字段名稱。默認值:當前類名 |
type | 對應的Hibernate類型 |
length | 字段長度 |
注意下面的例子運行,應該是不能自動生成代碼的,要把註釋說明文檔都刪掉纔可以,好象跟XDoclet文檔有衝突,我以前有次就是寫了些註釋文檔後就會出錯。
*
* @hibernate.class
* table="TUser"
* dynamic-update="true"
* dynamic-insert="true"
*
* @hibernate.discriminator column="user_type" type="integer"
*/
public class TUser implements Serializable {
......
}
//根類TUser 中,通過@hibernate.discriminator 指定了以"user_type"字段
//作爲識別字段。
/**
* @hibernate.subclass
* discriminator-value="1"
*/
public class SysAdmin extends TUser {
......
}
/**
* @hibernate.subclass
* discriminator-value="2"
*/
public class SysOperator extends TUser {
......
}
//SysAdmin 和SysOperator 均繼承自TUser,其discriminator-value 分別設置
//爲"1"和"2",運行期Hibernate 在讀取t_user 表數據時,會根據其user_type 字段進行
//判斷,如果是1 的話則映射到SysAdmin類,如果是2 映射到SysOperator 類。
@hibernate.subclass,顧名思義,@hibernate.subclass與@hibernate.class
不同之處就在於,@hibernate.subclass 描述的是一個子類,實際上,這兩個Tag
除去名稱不同外,並沒有什麼區別。
描述POJO 中關鍵字段與數據庫表主鍵之間的映射關係。
column | 主鍵字段名,默認值:當前類名 |
type | 字段類型。Hibernate總是使用對象型數據類型作爲字段類型,如int對應Integer,因此這裏將id設爲基本類型[如int]以避免對 象創建的開銷的思路是沒有實際意義的,即使這裏設置爲基本類型,Hibernate內部還是會使用對象型數據對其進行處理,只是返回數據的時候再轉換爲基本類型而已。 |
length | 字段長度 |
unsaved-value | 用於對象是否已經保存的判定值。 |
generator-class | 主鍵產生方式(詳見Hibernate QuickStart中關於MiddleGen的相關說明)取值可爲下列值中的任意一個: assigned,hilo,seqhilo, increment, identity, sequence, native, uuid.hex, uuid.string, foreign |
column | 數據庫表字段名,默認值:當前類名 |
type | 字段類型 |
length | 字段長度 |
not-null | 字段是否允許爲空 |
unique | 字段是否唯一(是否允許重複值) |
insert Insert | 操作時是否包含本字段數據,默認:true |
update Update | 操作時是否包含本字段數據,默認:true |
Declares a parent reference
inverse | If inverse collection 默認:false 。 inverse="false"表示主控方在 該類 |
table | Defaults to role name: the name of the collection table (not used for one-to-many associations) |
cascade | Specifies which operations should be cascaded from the parent object to the associated object Valid options are: all,none,save-update,delete,all-delete-orphan,delete-orphan |
sort | Specify a sorted collection with natural sort order or a given comparator class |
access | The strategy Hibernate should use for accessing the property value. Default value(s): property Valid options are: field,property,ClassName |
class | The name of the associated class |
property-ref | bi-directional reference to one-to-one table that holds the foreign key |
foreign-key | The name of the foreign key constraint to associate with this association. |
constrained | Is there a foreign key constraint |
outer-join | Enable outer-join fetching for this association when hibernate.use_outer_join is set Default value(s): auto Valid options are: true , false, auto |
cascade | 同 |
acess | 同 |
column | The name of the mapped database table column |
class | The name of the associated class |
not-null | If the column is not nullable Default value(s): false |
insert | Should the column appear in the SQL INSERT. Only applies for version >= 2.0 |
update | Should the column appear in the SQL UPDATE. Only applies for version >= 2.0 |
cascade | 同 |
acess | 同 |
列舉的是一些常用的,具體的可以去官方網站查找。
學習資源:
XDoclet 與Hibernate 映射
XDoclet @hibernate Tag Reference