[hibernate]xdoclet 生成 hbm.xml [one-to-many单向外键]

请大家直接看代码吧.天太热了... 
环境是eclipse ganymede , jdk 6.0, hibernate 3.2, xdoclet1.2.3,ant 1.6.5,mysql5.0

 通过Car 和 Owner的关系可以看出1个Owner可以有0..*个Car

import java.io.Serializable;

/**
 * @hibernate.class table="car"
 * @author Charles King
 *
 */
public class Car implements Serializable{
	private Integer carId;
	
	private String carName;

	/**
	 * @hibernate.id generator-class = "native" column="car_id"
	 * @return
	 */
	public Integer getCarId() {
		return carId;
	}

	public void setCarId(Integer carId) {
		this.carId = carId;
	}

	/**
	 * @hibernate.property column="car_name"
	 * @return
	 */
	public String getCarName() {
		return carName;
	}

	public void setCarName(String carName) {
		this.carName = carName;
	}
	
	
	
}

 

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * @hibernate.class table = "owner"
 * @author Charles King
 *
 */
public class Owner implements Serializable {

	private Integer ownerId;
	
	private String ownerName;
	
	private Set cars = new HashSet();

	/**
	 * @hibernate.id generator-class = "native" column = "owner_id"
	 * @return
	 */
	public Integer getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(Integer ownerId) {
		this.ownerId = ownerId;
	}

	/**
	 * @hibernate.property column = "owner_name"
	 * @return
	 */
	public String getOwnerName() {
		return ownerName;
	}

	public void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}

	/**
	 * @hibernate.set inverse = "true" cascade = "all"
	 * @hibernate.collection-key column = "car_id"
	 * @hibernate.collection-one-to-many class = "net.alp.model.Car"
	 * @return
	 */
	public Set getCars() {
		return cars;
	}

	public void setCars(Set cars) {
		this.cars = cars;
	}
	
	
}

 

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project>
	<!-- set properties -->
	<property name="src.dir" value="src"></property>
	<property name="lib.dir" value="lib"></property>
	<property name="build.dir" value="build"></property>
	<property name="dist.dir" value="dist"></property>
	<property name="project_name" value="xdoclet"></property>
	<property file="hibernate.properties"></property>

	<path id="lib.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar"/>
		</fileset>
	</path>
	
	<path id="src.classpath">
		<pathelement location="${src.dir}"/>
	</path>
	
	<path id="runtime.classpath">
		<path refid="lib.classpath"></path>
		<path refid="src.classpath"></path>
	</path>
	<!-- ================================= 
          target: genate-hiberate3.x            
         ================================= -->
	<target name="gen-hib">
		<taskdef
			name="hibernatedoclet"
			classname="xdoclet.modules.hibernate.HibernateDocletTask"
			classpathref="runtime.classpath"
			>
		</taskdef>
		<hibernatedoclet
			destdir="${src.dir}"
			verbose="true"
			>
			<fileset dir="${src.dir}">
				<include name="**/*.java"/>
			</fileset>
			<hibernate version="3.0"/>
			<hibernatecfg
				dialect="${hibernate.dialect}"
				jdbcUrl="${hibernate.connection.url}"
				driver="${hibernate.connection.driver_class}"
				username="${hibernate.connection.username}"
				password="${hibernate.connection.password}"
				showSql="false"
				version="3.0"
			/>
		</hibernatedoclet>
	</target>
</project>

 

hibernate.properties

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.url=jdbc:mysql://localhost:3306/test
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.username=root
hibernate.connection.password=root

 

 表建好了,这是用ant生成的SQL

    alter table car 
        drop 
        foreign key FK17FD49D5CA7DA;

    drop table if exists car;

    drop table if exists owner;

    create table car (
        car_id integer not null auto_increment,
        car_name varchar(255),
        primary key (car_id)
    );

    create table owner (
        owner_id integer not null auto_increment,
        owner_name varchar(255),
        primary key (owner_id)
    );

    alter table car 
        add index FK17FD49D5CA7DA (car_id), 
        add constraint FK17FD49D5CA7DA 
        foreign key (car_id) 
        references owner (owner_id);

 

生成这段的SQL在下面补上

	<!-- ================================= 
          target: gen sql              
         ================================= -->
    <target name="gen sql">
        <mkdir dir="sql"/>
    	<java classname="org.hibernate.tool.hbm2ddl.SchemaExport" fork="true">
    		<classpath refid="runtime.classpath"></classpath>
    		<arg value="--text"/>
    		<arg value="--quiet"/>
    		<arg value="--format"/>
    		<arg value="--output=sql/schema.sql"/>
    		<arg value="--properties=${basedir}/hibernate.properties"/>
    		<arg value="--delimiter=;"/>
    		<arg value="${src.dir}/net/alp/model/*.xml"/>
    	</java>
    </target>

 

你可能感兴趣的:(sql,mysql,Hibernate,xml,ant)