通过XDoclet可以我们的精力放在编写java源文件上。
具体来说就是:
只有Java: java--->XDoclet(hibernatedoclet)--->Hbm---->SchemaExport(schemaexport,hbm2ddl)---->数据表
1:java源文件编写
420){this.resized=true;this.style.width=420;}" align=top> * Created on 2006-4-7
420){this.resized=true;this.style.width=420;}" align=top> */
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top> package com.entity;
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>420){this.resized=true;this.style.width=420;}" align=top> /** */ /**
420){this.resized=true;this.style.width=420;}" align=top> * @author jkallen
420){this.resized=true;this.style.width=420;}" align=top> * @hibernate.class lazy="true" table="syn_dept"
420){this.resized=true;this.style.width=420;}" align=top> * @hibernate.cache usage="read-write"
420){this.resized=true;this.style.width=420;}" align=top> */
420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> public class SynDepartment 420){this.resized=true;this.style.width=420;}"> {
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>420){this.resized=true;this.style.width=420;}" align=top> /** */ /** 主键 id */
420){this.resized=true;this.style.width=420;}" align=top> private Long id;
420){this.resized=true;this.style.width=420;}" align=top>420){this.resized=true;this.style.width=420;}" align=top> /** */ /** 部门名称 */
420){this.resized=true;this.style.width=420;}" align=top> private String code_name;
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>420){this.resized=true;this.style.width=420;}" align=top> /** */ /**
420){this.resized=true;this.style.width=420;}" align=top> * @return Returns the id.
420){this.resized=true;this.style.width=420;}" align=top> * @hibernate.id generator-class="native" column="id"
420){this.resized=true;this.style.width=420;}" align=top> */
420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> public Long getId() 420){this.resized=true;this.style.width=420;}"> {
420){this.resized=true;this.style.width=420;}" align=top> return id;
420){this.resized=true;this.style.width=420;}" align=top> }
420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> public void setId(Long id) 420){this.resized=true;this.style.width=420;}"> {
420){this.resized=true;this.style.width=420;}" align=top> this .id = id;
420){this.resized=true;this.style.width=420;}" align=top> }
420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> /** */ /**
420){this.resized=true;this.style.width=420;}" align=top> * @return Returns the code_name.
420){this.resized=true;this.style.width=420;}" align=top> * @hibernate.property column = "code_name"
420){this.resized=true;this.style.width=420;}" align=top> */
420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> public String getCode_name() 420){this.resized=true;this.style.width=420;}"> {
420){this.resized=true;this.style.width=420;}" align=top> return code_name;
420){this.resized=true;this.style.width=420;}" align=top> }
420){this.resized=true;this.style.width=420;}" align=top> 420){this.resized=true;this.style.width=420;}" align=top> public void setCode_name(String code_name) 420){this.resized=true;this.style.width=420;}"> {
420){this.resized=true;this.style.width=420;}" align=top> this .code_name = code_name;
420){this.resized=true;this.style.width=420;}" align=top> }
420){this.resized=true;this.style.width=420;}" align=top>}
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top>
这里用到了几种@hibernate标记的用法
@hibernate.class标记指定类的映射代码,lazy="true" table="syn_dept"则如
hibernate的映射文件class元素的属性值具有相同的意义
@hibernate.id标记指定类的OID映射代码
@hibernate.property标记指定类的属性映射代码
另外还可能用到@hibernate.set(如一对多的情况下)
2:XDoclet--->Hbm(写在build.xml文件中,ANT运行)
420){this.resized=true;this.style.width=420;}" align=top> depends ="compileEntity"
420){this.resized=true;this.style.width=420;}" align=top> description ="Generate hibernate mapping documents" >
420){this.resized=true;this.style.width=420;}" align=top> < hibernatedoclet destdir ="${generated.dir}" >
420){this.resized=true;this.style.width=420;}" align=top> < fileset dir ="${src.dir}" >
420){this.resized=true;this.style.width=420;}" align=top> < include name ="**/entity/*.java" />
420){this.resized=true;this.style.width=420;}" align=top> </ fileset >
420){this.resized=true;this.style.width=420;}" align=top> < hibernate version ="2.0" />
420){this.resized=true;this.style.width=420;}" align=top> </ hibernatedoclet >
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top> < copy todir ="${classes.dir}" >
420){this.resized=true;this.style.width=420;}" align=top> < fileset dir ="${generated.dir}" />
420){this.resized=true;this.style.width=420;}" align=top> </ copy >
420){this.resized=true;this.style.width=420;}" align=top> </ target >
通过hibernatedoclet就可以生成SynDepartment.hbm.xml映射文件
fileset顾名思义就是过滤文件了。
注:compileEntity--编译java源文件(自定义)
3:SchemaExport---->数据表
420){this.resized=true;this.style.width=420;}" align=top> < schemaexport properties ="${classes.dir}/hibernate.properties"
420){this.resized=true;this.style.width=420;}" align=top> quiet ="no" text ="no" drop ="no"
420){this.resized=true;this.style.width=420;}" align=top> delimiter ="
go
" output ="${sql.dir}/${synup.sql.file}"
420){this.resized=true;this.style.width=420;}" align=top> >
420){this.resized=true;this.style.width=420;}" align=top> < fileset refid ="hibernate.synup.mapping.files" />
420){this.resized=true;this.style.width=420;}" align=top> </ schemaexport >
420){this.resized=true;this.style.width=420;}" align=top> < echo message ="Output sql to file: ${sql.dir}/${sql.file}" />
420){this.resized=true;this.style.width=420;}" align=top> </ target >
420){this.resized=true;this.style.width=420;}" align=top> < fileset id ="hibernate.synup.mapping.files" dir ="${classes.dir}" >
420){this.resized=true;this.style.width=420;}" align=top> < include name ="**/entity/*.hbm.xml" />
420){this.resized=true;this.style.width=420;}" align=top> </ fileset >
quiet:如果为yes,表示不把子DDL脚本输出到控制台
drop:如果为yes,只执行删除数据库中的操作,但不创建新的表
text:如果为yes,只会生成DDL脚本文件,但不会在数据库中执行DDL脚本
output:指定存放DDL脚本文件的目录
config:设定基于XML格式的配置文件, hbm2ddl(schemaexport)工具从这个文件中读取数据库的配置信息
properties:设定基于java属性文件格式的配置文件,hbm2ddl(schemaexport)工具从这个文件中读取DB的配置信息
format:设定DDL脚本中SQL语句的格式
delimiter:为DDL脚本设置行结束符
在ANT中执行:
<target name="initOnlySynup" depends="toHbm,toddl">
</target>
OK,最后生成的映射文件如下:
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top> <! DOCTYPE hibernate-mapping PUBLIC
420){this.resized=true;this.style.width=420;}" align=top> "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
420){this.resized=true;this.style.width=420;}" align=top> "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top> < hibernate-mapping >
420){this.resized=true;this.style.width=420;}" align=top> < class
420){this.resized=true;this.style.width=420;}" align=top> name ="com.SynDepartment"
420){this.resized=true;this.style.width=420;}" align=top> table ="syn_dept"
420){this.resized=true;this.style.width=420;}" align=top> dynamic-update ="false"
420){this.resized=true;this.style.width=420;}" align=top> dynamic-insert ="false"
420){this.resized=true;this.style.width=420;}" align=top> >
420){this.resized=true;this.style.width=420;}" align=top> < cache usage ="read-write" />
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top> < id
420){this.resized=true;this.style.width=420;}" align=top> name ="id"
420){this.resized=true;this.style.width=420;}" align=top> column ="id"
420){this.resized=true;this.style.width=420;}" align=top> type ="java.lang.Long"
420){this.resized=true;this.style.width=420;}" align=top> >
420){this.resized=true;this.style.width=420;}" align=top> < generator class ="native" >
420){this.resized=true;this.style.width=420;}" align=top> </ generator >
420){this.resized=true;this.style.width=420;}" align=top> </ id >
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top> < property
420){this.resized=true;this.style.width=420;}" align=top> name ="code_name"
420){this.resized=true;this.style.width=420;}" align=top> type ="java.lang.String"
420){this.resized=true;this.style.width=420;}" align=top> update ="true"
420){this.resized=true;this.style.width=420;}" align=top> insert ="true"
420){this.resized=true;this.style.width=420;}" align=top> access ="property"
420){this.resized=true;this.style.width=420;}" align=top> column ="code_name"
420){this.resized=true;this.style.width=420;}" align=top> />
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top> <!--
420){this.resized=true;this.style.width=420;}" align=top> To add non XDoclet property mappings, create a file named
420){this.resized=true;this.style.width=420;}" align=top> hibernate-properties-SynDepartment.xml
420){this.resized=true;this.style.width=420;}" align=top> containing the additional properties and place it in your merge dir.
420){this.resized=true;this.style.width=420;}" align=top> -->
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top> </ class >
420){this.resized=true;this.style.width=420;}" align=top>
420){this.resized=true;this.style.width=420;}" align=top> </ hibernate-mapping >
420){this.resized=true;this.style.width=420;}" align=top>
控制台中部分信息如下:
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] go
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] drop sequence hibernate_sequence
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] go
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] create table syn_dept (
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] id number(19,0) not null,
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] code_name varchar2(255),
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] primary key (id)
420){this.resized=true;this.style.width=420;}" align=top>[schemaexport] )
DB中已经生成syn_dept表了,快去看下吧!
关于Xdoclet 中的hibernate标签更多信息可以参考:
http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html#@hibernate_collection-key__0__1_
我还在一个网友的博客上看到了他对此的汉化:
http://blog.csdn.net/fasttalk/archive/2005/09/19/484615.aspx
参考资料:Hibernate持久化技术详解
来源:http://www.blogjava.net/Jkallen/archive/2006/04/10/40305.html