下面举例说明(from http://forum.javaeye.com/viewtopic.php?t=2991)
package com.javamodel.hibernate;
import java.util.HashSet;
import java.util.Set;
/**
* @hibernate.class
* table="author"
* dynamic-update="true"
* dynamic-insert="true"
* 这个类对应的表是author
*/
public class Author{
private String id ;
private String alias = null;
private Person person = null;
private Set publications = new HashSet();
private Set works = new HashSet();
/**
* @hibernate.id
* unsaved-value="null" generator-class="foreign"
* 定义外键
* @hibernate.generator-param
* name="property" value="person"
* 对应的Author对应的属性
*/
public String getId() {
return id;
}
public void setId(String i) {
id = i;
}
/**
* @hibernate.property
* length="20"
* 声明表中对应的字段
*/
public String getAlias() {
return alias;
}
public void setAlias(String string) {
alias = string;
}
/**
* @hibernate.one-to-one
* cascade="all" constrained="true"
* 在Author与Person之间,声明one-to-one的关联关系
*/
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
/**
* @hibernate.set
* lazy="true" inverse="true" cascade="all"
* 定义Hibernate <set> collection
* @hibernate.collection-key
* column="authorid"
* 对应表中的字段
* @hibernate.collection-one-to-many
* class="com.javamodel.hibernate.Publication"
* 在Author与Publication之间,声明one-to-many的关联关系
*/
public Set getPublications() {
return publications;
}
public void setPublications(Set set) {
publications = set;
}
/**
* @hibernate.set
* lazy="true"
* table="author_work"
* @hibernate.collection-key
* column="author_id"
* @hibernate.collection-many-to-many
* column="work_id"
* class="com.javamodel.hibernate.Work"
*/
public Set getWorks() {
return works;
}
public void setWorks(Set set) {
works = set;
}
}
感觉用起来还是比较麻烦,还是根据数据库表自动生成来得方便。