假设有一个实体类,用于存放用户信息,其定义如下:
package events;
import java.util.*;
public class Person {
private Long id;
private int age;
private String firstname;
private String lastname;
public Person() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
private Set emailAddresses = new HashSet();
public Set getEmailAddresses() {
return emailAddresses;
}
public void setEmailAddresses(Set emailAddresses) {
this.emailAddresses = emailAddresses;
}
private Set events = new HashSet();
// Defensive, convenience methods
protected Set getEvents() {
return events;
}
protected void setEvents(Set events) {
this.events = events;
}
public void addToEvent(Event event) {
this.getEvents().add(event);
event.getParticipants().add(this);
}
public void removeFromEvent(Event event) {
this.getEvents().remove(event);
event.getParticipants().remove(this);
}
}
则相应的描述POJO--->Person.hbm.xml中的配置信息如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="events.Person" table="PERSON">
<id name="id" column="PERSON_ID">
<generator class="identity" />
</id>
<property name="age" />
<property name="firstname" />
<property name="lastname" />
<set name="events" table="PERSON_EVENT">
<key column="PERSON_ID" />
<many-to-many column="EVENT_ID" class="events.Event" />
</set>
<set name="emailAddresses" table="PERSON_EMAIL_ADDR">
<key column="PERSON_ID" />
<element type="string" column="EMAIL_ADDR" />
</set>
</class>
</hibernate-mapping>
说明:
1.class 节点
name: 类名
table: 类对应表名,默认为类名称
dynamic-update: 生成更新字段时,只包含发生变动的字段,默认为false。
dynamic-insert: 生成insert语句时仅包含非null字段
Proxy: 代理类,默认为空
discriminator-value: 子类辨别标识用于多态支持
where: 通过限定条件查询结果集。如:查询有籍在校学生的信息可以使用"where studentstatus='0'"
|
2.id节点
1.column 字段名称
2.type 字段类型
3.length 字段长度
4.unsaved-value 用于判断对象值是否已经保存
5.generator-class 主键产生方式
assigned
hilo
seqhilo
increment
identity
sequence
native
uuid.hex
uuid.string
foreign
|
3.property 节点
1.column 数据库表字段名称
2.type 类型
3.length 长度
4.not-null 字段是否允许为空
5.unique 字段是否允许唯一(是否允许重复值)
6.insert insert操作时,是否允许包含本字段数值
7.update update操作时,是否包含本字段数据
|
Event.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native" />
</id>
<property name="date" type="timestamp" column="EVENT_DATE" />
<property name="title" />
<set name="participants" table="PERSON_EVENT" inverse="true">
<key column="EVENT_ID" />
<many-to-many column="PERSON_ID" class="events.Person" />
</set>
</class>
</hibernate-mapping>