POJO to DDL 关键字 xdoclet ant hbm2ddl schemaexport

Assumptions:
Download hibernate and xdoclet, and create one java project in eclipse.

1, Create one POJO class with xdoclet

package com.loader.model;

import java.io.Serializable;

/**
* @hibernate.class table="PRODUCT"
*/
public class Product implements Serializable{

private Long id;
private String no;
private String name;

public void setId(Long id) {
this.id = id;
}

/**
* @hibernate.id column="ID" generator-class="native"
*/
public Long getId() {
return id;
}

/**
* @hibernate.property
*/
public String getNo() {
return no;
}

public void setNo(String no) {
this.no = no;
}

/**
* @hibernate.property
*/
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}


2, Create one build.xml to run HibernateDocletTask&SchemaExportTask

<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="schemaexport" basedir=".">
<property name="src.dir" value="${basedir}/src" />
<property name="mapping.dir" value="${basedir}/mappings" />
<property name="xdoclet.home" value="C:\gyl\materials\Java\xdoclet\xdoclet-1.2.3" />

<path id="project.classpath">
<pathelement path="${java.class.path}" />
<fileset dir="${xdoclet.home}/lib">
<include name="**/*.jar" />
</fileset>
<fileset dir="${basedir}/lib">
<include name="**/*.jar" />
</fileset>
</path>

<taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="project.classpath" />

<target name="xdoclet">
<hibernatedoclet destdir="${src.dir}" excludedtags="@version,@author,@todo" force="true" mergedir="{basedir}/merge/" verbose="true">
<fileset dir="src">
<include name="**/model/*.java" />
</fileset>
<hibernate version="3.0" />
</hibernatedoclet>
</target>

<target name="schemaexport" depends="xdoclet">
<taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="project.classpath"/>

<schemaexport config="${basedir}/etc/hibernate_export.cfg.xml" text="yes" delimiter=";" output="schema-export.sql">
<fileset dir="src">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaexport>
</target>

</project>

3, Run the build.xml, the DDL will be generated as below:

    drop table if exists PRODUCT;

    create table PRODUCT (
        ID bigint not null auto_increment,
        no varchar(255),
        name varchar(255),
        primary key (ID)
    );

你可能感兴趣的:(java,eclipse,Hibernate,xml,ant)