使用XDoclet
XDoclet项目给Java带来了面向属性的编程概念。XDoclet利用Javadoc标签格式(@attribute)来指定类、字段,或者方法级的元数据属性。XDoclet被实现为生成Hibernate XML元数据(或者其他东西,取决于插件)的一个Ant目标,作为构建过程的一部分。用XDoclet创建Hibernate XML映射文档很简单;不用手工编写,而是用定制的Javadoc标签给持久化类的Java源代码做上标记。
XDoclet下载地址:XDoclet2下载地址
/** * 用户实体类 * * @hibernate.mapping default-cascade="true" * @hibernate.class table="TBL_USER" * */ public class User { private Integer id;// 用户编号 private String username;// 用户名 private String password;// 密码 public User() { super(); } public User(String username, String password) { super(); this.username = username; this.password = password; } /** * @hibernate.id generator-class="native" column="USER_ID" type="integer" * @return */ public Integer getId() { return id; } @SuppressWarnings("unused") private void setId(Integer id) { this.id = id; } /** * @hibernate.property column="UNAME" type="string" length="50" not-null="false" * @return */ public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } /** * @hibernate.property column="UPWD" type="string" length="50" not-null="false" * @return */ public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }build.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project name="XDoclet-Hibernate" basedir="."> <!--后面的构建任务中可能会用到的一些基本属性--> <property name="project.lib.dir" value="${basedir}/lib" /> <property name="project.src.dir" value="${basedir}/src" /> <property name="xdoclet.home" value="E:/Jar/xdoclet-plugins-dist-1.0.4" /> <property file="hibernate.properties" /> <!-- =================================================================== --> <!-- Define the class path --> <!-- =================================================================== --> <path id="xdoclet.task.classpath"> <fileset dir="${xdoclet.home}/lib"> <include name="**/*.jar" /> </fileset> </path> <!-- 引用引入的jar包--> <taskdef name="xdoclet" classname="org.xdoclet.ant.XDocletTask" classpathref="xdoclet.task.classpath" /> <!-- =================================================================== --> <!-- 生成Hibernate映射文件 --> <!-- =================================================================== --> <target name="gen-mapping"> <xdoclet> <fileset dir="${project.src.dir}"> <include name="**/*.java" /> </fileset> <component classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin" version="3.0" destdir="${project.src.dir}" /> </xdoclet> </target> <!-- =================================================================== --> <!-- 生成Hibernate配置文件 --> <!-- =================================================================== --> <target name="gen-configuration"> <xdoclet> <fileset dir="${project.src.dir}"> <include name="**/*.java"/> </fileset> <component classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin" destdir="${project.src.dir}" version="3.0" hbm2ddlauto="create" jdbcurl="${hibernare.connection.url}" jdbcdriver="${hibernate.driver_class}" jdbcusername="${hibernate.connection.username}" jdbcpassword="${hibernate.connection.password}" dialect="${hibernate.dialect}" showsql="true" /> </xdoclet> </target> </project>