通过XDoclet(ant)生成Hibernate映射文件
0
通过XDoclet可以我们的精力放在编写java源文件上。 1:java源文件编写 /**/ /*
* 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; /** */ /** 部门名称 */ 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; } }
2:XDoclet--->Hbm(写在build.xml文件中,ANT运行) < 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 >
3:SchemaExport---->数据表 < 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 >
<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate./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 >
[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中已经生成syn_dept表了,快去看下吧! |
|