使用XDoclet生成hibernate映射文件方法。不知道是our government的问题还是sourceforge.net又看咱们不顺眼了,总之sourceforge.net上不去了,没法去它那下xdoclet,可以到这里来下,ftp://sourceforge.nchc.org.tw/x/xd/xdoclet/xdoclet/1.2.3/,下它的xdoclet-bin-1.2.3.zip文件,这里面包含了所有的JAR包和文档,xdoclet-lib-1.2.3.zip这里面只包含了jar包,没有包含文档等其他的东西。
下好之后,然后把zip包中的
commons-collections-2.0.jar commons-logging.jar
xdoclet-1.2.3.jar xdoclet-hibernate-module-1.2.3.jar
xdoclet-xdoclet-module-1.2.3.jar xjavadoc-1.1.jar
这些文件考到项目根目录下的lib文件夹里。
然后在编写的java源文件中加入@标记,这里列个我照书做的小例子的三个java源文件,这三个文件都放在项目根目录下的src目录 下。
Customer.java:
package entity;
import java.util.Set;
import java.util.HashSet;
/**
*@hibernate.class
*table=”customers”
*/
public class Customer {
private Long id;
private String name;
private Address address;
private Set orders=new HashSet();
private void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
/**
*@hibernate.id
*column=”id”
*unsaved-value=”null”
*generator-class=”increment”
*/
public Long getId() {
return (this.id);
}
/**
*@hibernate.property
*column=”name”
*length=”15″
*not-null=”true”
*update=”false”
*/
public String getName() {
return (this.name);
}
/**
*@hibernate.component
*/
public Address getAddress() {
return (this.address);
}
public void setOrders(Set orders) {
this.orders = orders;
}
/**
*@hibernate.set
*inverse=”true”
*lazy=”true”
*cascade=”save-update”
*@hibernate.collection-key
*column=”cid”
*@hibernate.collection-one-to-many
*class=”entity.Order”
*/
public Set getOrders() {
return (this.orders);
}
}
Address:
package entity;
public class Address {
private String zipCode;
private String addressName;
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public void setAddressName(String addressName) {
this.addressName = addressName;
}
/**
*@hibernate.property
*column=”zipcode”
*length=”6″
*not-null=”true”
*update=”false”
*/
public String getZipCode() {
return (this.zipCode);
}
/**
*@hibernate.property
*column=”addressname”
*length=”200″
*not-null=”true”
*update=”false”
*/
public String getAddressName() {
return (this.addressName);
}
}
Order:
package entity;
/**
*@hibernate.class
*table=”orders”
*/
public class Order {
private Long id;
private Customer customer;
private String orderCode;
private void setId(Long id) {
this.id = id;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public void setOrderCode(String orderCode) {
this.orderCode = orderCode;
}
/**
*@hibernate.id
*column=”id”
*unsaved-value=”null”
*generator-class=”increment”
*/
public Long getId() {
return (this.id);
}
/**
*@hibernate.many-to-one
*column=”cid”
*cascade=”none”
*not-null=”true”
*/
public Customer getCustomer() {
return (this.customer);
}
/**
*@hibernate.property
*column=”ordercode”
*length=”15″
*not-null=”true”
*update=”false”
*/
public String getOrderCode() {
return (this.orderCode);
}
}
最后编写ant的工程文件build.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<project name=”text xdoclet” default=”prepare” basedir=”.”>
<path id=”libs”>
<fileset dir=”lib”>
<include name=”*.jar”/>
</fileset>
</path>
<target name=”xdoclet”>
<taskdef name=”hibernatedoclet” classname=”xdoclet.modules.hibernate.HibernateDocletTask” classpathref=”libs”/>
<hibernatedoclet destdir=”src” excludedtags=”@version,@author,@todo” force=”true” mergedir=”merge”>
<fileset dir=”src”>
<include name=”**/*.java”/>
</fileset>
<hibernate version=”2.0″/>
</hibernatedoclet>
</target>
</project>
一切都准备完毕,在命令行下进入项目的根目录,然后执行ant xdoclet,就能生成相应的hbm.xml文件了。