通過(guò)XDoclet可以我們的精力放在編寫(xiě)java源文件上。
具體來(lái)說(shuō)就是:
只有Java: java--->XDoclet(hibernatedoclet)--->Hbm---->SchemaExport(schemaexport,hbm2ddl)---->數(shù)據(jù)表
1:java源文件編寫(xiě)
/**/ /*
* Created on 2006-4-7
*/
package com.entity;
/** */ /**
* @author jkallen
* @hibernate.class lazy="true" table="syn_dept"
* @hibernate.cache usage="read-write"
*/
public class SynDepartment {
/** */ /** 主鍵 id */
private Long id;
/** */ /** 部門(mén)名稱(chēng) */
private String code_name;
/** */ /**
* @return Returns the id.
* @hibernate.id generator-class="native" column="id"
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this .id = id;
}
/** */ /**
* @return Returns the code_name.
* @hibernate.property column = "code_name"
*/
public String getCode_name() {
return code_name;
}
public void setCode_name(String code_name) {
this .code_name = code_name;
}
}
這里用到了幾種@hibernate標(biāo)記的用法
@hibernate.class標(biāo)記指定類(lèi)的映射代碼,lazy="true" table="syn_dept"則如
hibernate的映射文件class元素的屬性值具有相同的意義
@hibernate.id標(biāo)記指定類(lèi)的OID映射代碼
@hibernate.property標(biāo)記指定類(lèi)的屬性映射代碼
另外還可能用到@hibernate.set(如一對(duì)多的情況下)
2:XDoclet--->Hbm(寫(xiě)在build.xml文件中,ANT運(yùn)行)
< 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 >
通過(guò)hibernatedoclet就可以生成SynDepartment.hbm.xml映射文件
fileset顧名思義就是過(guò)濾文件了。
注:compileEntity--編譯java源文件(自定義)
3:SchemaExport---->數(shù)據(jù)表
< target name ="toddl" depends ="init" >
< schemaexport properties ="${classes.dir}/hibernate.properties"
quiet ="no" text ="no" drop ="no"
delimiter ="
go
" 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 >
通過(guò)schemaexport就向DB中生成table了。其中可能用到如下的一些屬性:
quiet:如果為yes,表示不把子DDL腳本輸出到控制臺(tái)
drop:如果為yes,只執(zhí)行刪除數(shù)據(jù)庫(kù)中的操作,但不創(chuàng)建新的表
text:如果為yes,只會(huì)生成DDL腳本文件,但不會(huì)在數(shù)據(jù)庫(kù)中執(zhí)行DDL腳本
output:指定存放DDL腳本文件的目錄
config:設(shè)定基于XML格式的配置文件, hbm2ddl(schemaexport)工具從這個(gè)文件中讀取數(shù)據(jù)庫(kù)的配置信息
properties:設(shè)定基于java屬性文件格式的配置文件,hbm2ddl(schemaexport)工具從這個(gè)文件中讀取DB的配置信息
format:設(shè)定DDL腳本中SQL語(yǔ)句的格式
delimiter:為DDL腳本設(shè)置行結(jié)束符
在ANT中執(zhí)行:
<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 >
控制臺(tái)中部分信息如下:
[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中已經(jīng)生成syn_dept表了,快去看下吧!