利用Ant和XDoclet自动产生映射文件例子
2009-09-23 00:00:00 来源:WEB开发网//User.java
在企业中model类的属性可能有几百个而且还可能继承了很多属性,这样的model如果手写映射文件岂不是很大的工程!
这里介绍Ant+XDoclet配合来自动生成映射文件。
必备:Ant和XDocle的jar包。
操作很简单,自己写一个model类例如People.java,但是要自动生成映射文件这个类需要有注释,写一个build.xml文件,
下载Xdoclet,网址:http://xdoclet.sourceforge.net/
新建包com.test.model,存放实体类Group,User
package dbdemo;
import java.util.Date;
import java.util.Set;
/**
* @hibernate.class table="Users"
*
* @author Ethan
*
* Represents a User
*/
public class User {
private String userID;
private String userName;
private String password;
private String emailAddress;
private Date lastLogon;
private Set contacts;
private Set books;
private Address address;
/**
* @hibernate.property column="EmailAddress" type="string"
* @return String
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* @hibernate.property column="LastLogon" type="date"
* @return Date
*/
public Date getLastLogon() {
return lastLogon;
}
/**
* @hibernate.property column="Password" type="string"
* @return String
*/
public String getPassword() {
return password;
}
/**
* @hibernate.id generator-class="assigned" type="string"
* column="LogonID"
* @return String
*/
public String getUserID() {
return userID;
}
/**
* @hibernate.property column="Name" type="string"
* @return String
*/
public String getUserName() {
return userName;
}
/**
* @param string
*/
public void setEmailAddress(String string) {
emailAddress = string;
}
/**
* @param string
*/
public void setLastLogon(Date date) {
lastLogon = date;
}
/**
* @param string
*/
public void setPassword(String string) {
password = string;
}
/**
* @param string
*/
public void setUserID(String string) {
userID = string;
}
/**
* @param string
*/
public void setUserName(String string) {
userName = string;
}
/**
* @hibernate.set role="contacts" table="Contacts"
* cascade="all" readonly="true"
* @hibernate.collection-key column="User_ID"
* @hibernate.collection-one-to-many class="dbdemo.Contact"
* @return java.util.Set
*/
public Set getContacts() {
return contacts;
}
/**
* @param set
*/
public void setContacts(Set set) {
contacts = set;
}
/**
* @hibernate.set role="books" table="Book_User_Link"
* cascade="all" eadonly="true"
* @hibernate.collection-key column="UserID"
* @hibernate.collection-many-to-many
* class="dbdemo.Book" column="BookID"
* @return java.util.Set
*/
public Set getBooks() {
return books;
}
/**
* @param set
*/
public void setBooks(Set set) {
books = set;
}
/**
* @hibernate.one-to-one class="dbdemo.Address"
* @return dbdemo.Address
*/
public Address getAddress() {
return address;
}
/**
* @param address
*/
public void setAddress(Address address) {
this.address = address;
}
}
更多精彩
赞助商链接