读书人

施用XDoclet反向生成hibernate映射文件

发布时间: 2012-11-08 08:48:11 作者: rapoo

使用XDoclet反向生成hibernate映射文件和DDL

1 在编写vo文件时候在类和字段的注释上添加xdoclet使用的信息

import java.util.Calendar;import java.util.Set;import java.io.Serializable;/** * @author Chenhui * @hibernate.class table="TB_User" */public class User implements Serializable {private static final long serialVersionUID = -4208021481713010569L;private Long userID;private Organization organization;//机构private Organization department;//部门private String userSN;private String loginName;private String password;private String userName;private String email;private int sex;private String address;private Calendar birthday;private String postalCode;private String mobilePhone;private String QQ;private String MSN;private int status;private Set groups;private Set privileges;public static final int STATUS_USABLE=1;// ok to usepublic static final int STATUS_UNABLE=2;// deletedpublic static final int SEX_MALE=1;public static final int SEX_FEMALE=2;public User(){}public User(long uid){this.userID=uid;}/** * @return the userID * @hibernate.id column="UserID" generator-unsaved-value="null" */public Long getUserID() {return userID;}/** * @param userID the userID to set */public void setUserID(Long userID) {this.userID = userID;}/** * @return the userSN * @hibernate.property column="UserSN" length="10" non-null="true" unique="true" */public String getUserSN() {return userSN;}/** * @param userSN the userSN to set */public void setUserSN(String userSN) {this.userSN = userSN;}/** * @return the loginName * @hibernate.property column="LoginName" length="20" non-null="true" unique="true" */public String getLoginName() {return loginName;}/** * @param loginName the loginName to set */public void setLoginName(String loginName) {this.loginName = loginName;}/** * @return the password * @hibernate.property column="Password" length="20" non-null="true" unique="false" */public String getPassword() {return password;}/** * @param password the password to set */public void setPassword(String password) {this.password = password;}/** * @return the userName * @hibernate.property column="UserName" length="20" non-null="false" unique="false" */public String getUserName() {return userName;}/** * @param userName the userName to set */public void setUserName(String userName) {this.userName = userName;}/** * @return the sex * @hibernate.property column="Sex" */public int getSex() {return sex;}/** * @param sex the sex to set */public void setSex(int sex) {this.sex = sex;}/** * @return the email * @hibernate.property column="Email" length="40" non-null="false" unique="false" */public String getEmail() {return email;}/** * @param email the email to set */public void setEmail(String email) {this.email = email;}/** * @return the address * @hibernate.property column="Address" length="80" non-null="false" unique="false" */public String getAddress() {return address;}/** * @param address the address to set */public void setAddress(String address) {this.address = address;}/** * @return the birthday * @hibernate.property column="Birthday" */public Calendar getBirthday() {return birthday;}/** * @param birthday the birthday to set */public void setBirthday(Calendar birthday) {this.birthday = birthday;}/** * @return the postalCode * @hibernate.property column="PostalCode" length="10" non-null="false" unique="false" */public String getPostalCode() {return postalCode;}/** * @param postalCode the postalCode to set */public void setPostalCode(String postalCode) {this.postalCode = postalCode;}/** * @return the mobilePhone * @hibernate.property column="MobilePhone" length="20" non-null="false" unique="false" */public String getMobilePhone() {return mobilePhone;}/** * @param mobilePhone the mobilePhone to set */public void setMobilePhone(String mobilePhone) {this.mobilePhone = mobilePhone;}/** * @return the qQ * @hibernate.property column="QQ" length="20" non-null="false" unique="false" */public String getQQ() {return QQ;}/** * @param qq the qQ to set */public void setQQ(String qq) {QQ = qq;}/** * @return the mSN * @hibernate.property column="MSN" length="40" non-null="false" unique="false" */public String getMSN() {return MSN;}/** * @param msn the mSN to set */public void setMSN(String msn) {MSN = msn;}/** * @return the status * @hibernate.property column="Status" */public int getStatus() {return status;}/** * @param status the status to set */public void setStatus(int status) {this.status = status;}/** * @return the organization * @hibernate.many-to-one column="OrganizationID" non-null="false" lazy="true" */public Organization getOrganization() {return organization;}/** * @param organization the organization to set */public void setOrganization(Organization organization) {this.organization = organization;}/** * @return the department * @hibernate.many-to-one column="DepartmentID" non-null="false" lazy="true" */public Organization getDepartment() {return department;}/** * @param department the department to set */public void setDepartment(Organization department) {this.department = department;}/** * @return the groups * Group来管理关联关系,默认不抓取 * @hibernate.set table="TB_UserGroup" cascade="none" inverse="true" lazy="true"  * @hibernate.collection-key column="UserID" * @hibernate.collection-many-to-many column="GroupID"      *   */public Set getGroups() {return groups;}/** * @param groups the groups to set */public void setGroups(Set groups) {this.groups = groups;}/** * @return the privileges * User来管理关联关系,默认不抓取 * @hibernate.set table="TB_UserPrivilege" cascade="all" inverse="false" lazy="true"  * @hibernate.collection-key column="UserID" * @hibernate.collection-many-to-many column="PrivilegeID"      *   */public Set getPrivileges() {return privileges;}/** * @param privileges the privileges to set */public void setPrivileges(Set privileges) {this.privileges = privileges;}}

?2 编写ant配置文件

??? 注意,hibernate的配置文件不须再包含映射文件,否则会出现重复错误

<?xml version="1.0"?><project name="administration" default="hibernate" basedir="."><property name="admin.db" value="mysql"/><property name="admin.home"     value="${basedir}"/><property name="admin.lib"     value="${admin.home}/lib"/><property name="build.classes" value="${admin.home}/WebRoot/WEB-INF/classes"/><property name="hibernate.cfg" value="${admin.home}/Docs/hibernate_${admin.db}.cfg.xml"/>    <path id="myclasspath">    <fileset dir="${admin.lib}/hibernate3.2.6" includes="**/*.jar"/>    <fileset dir="${admin.lib}/xdoclet1.2.3" includes="**/*.jar"/>    <fileset dir="${admin.home}/lib" includes="**/*.jar" />      <pathelement path="WebRoot/WEB-INF/classes"/>    </path><!-- ================================================================== --><!-- == generate Hibernate mappings                                     --><!-- ================================================================== -->     <taskdef        name="hibernatedoclet"        classname="xdoclet.modules.hibernate.HibernateDocletTask"        classpathref="myclasspath"        />    <target name="hibernate" description="Generate mapping documents">        <echo>+---------------------------------------------------+</echo>        <echo>|                                                   |</echo>        <echo>| R U N N I N G   H I B E R N A T E D O C L E T     |</echo>        <echo>|                                                   |</echo>        <echo>+---------------------------------------------------+</echo>        <hibernatedoclet            destdir="${admin.home}/src"            excludedtags="@version,@author,@todo,@see"            addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"            force="false"            verbose="true">            <fileset dir="${admin.home}/src"><exclude name="com/vo/BaseObject.java" /><include name="com/vo/*.java" />            </fileset>        <hibernate validatexml="true" version="3.0"/>        </hibernatedoclet>    </target><!-- ================================================================== --><!-- == generate DDL with hibernate tools                               --><!-- ================================================================== --><target name="gen-ddl" description="Hibernate Tools generate DDL"><taskdef    name="hibernatetool"    classname="org.hibernate.tool.ant.HibernateToolTask"        classpathref="myclasspath" /><!-- generate database DDL --><hibernatetool destdir="${admin.home}/metadata/database"><classpath><path location="${admin.home}/webapp/WEB-INF/classes"/></classpath><configuration configurationfile="${hibernate.cfg}" ><fileset dir="${admin.home}/src/com/vo"><include name="**/*.hbm.xml"/></fileset></configuration><hbm2ddl export="false" delimiter=";" format="true" outputfilename="schema_${admin.db}.sql"/></hibernatetool></target></project>

?3 运行ant编译生成hbm文件和DDL,注意运行顺序

读书人网 >软件架构设计

热点推荐