第一步
User
id
name
group
Group
id:int
name:String
第二步
Xml代碼
<?xml version=
<!DOCTYPE hibernate
hiber/hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<many
</class>
</hibernate
Person[id
IdCard[id
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
<param name=
</generator>
</id>
<property name=
<one
</class>
</hibernate
只需要在IdCard 的XML中添加<one
Person[id
IdCard[id
Xml代碼
Person
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</generator>
</id>
<property name=
<many
</class>
</hibernate
只需要在另一端(IdCard)加入<one
示意
Xml代碼
Id
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<one
</class>
</hibernate
Classes[id
Student[id
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<set name=
<key column=
<one
</set>
</class>
</hibernate
缺點
(一對多)
(多對一)
改進 采用 一對多雙向關聯
Xml代碼
<set name=
<set name=
這樣的好處是 存入數據讓多的一端進行維護
單向(采用第三方表)
Role[id
User[id
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<set name=
<key column=
<many
</set>
</class>
</hibernate
雙向
Role[id
User[id
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<set name=
<key column=
<many
</set>
</class>
</hibernate
第一類繼承映射
Animal
private int id
private String name
private Boolean sex
public class Bird extends Animal
private int height
public class Pig extends Animal
private int weight
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<discriminator column=
<property name=
<property name=
<subclass name=
<property name=
</subclass>
<subclass name=
<property name=
</subclass>
</class>
</hibernate
第二類mapping
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<property name=
<joined
<key column=
<property name=
</joined
<joined
<key column=
<property name=
</joined
</class>
</hibernate
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<property name=
<union
<property name=
</union
<union
<property name=
</union
</class>
</hibernate
component不是實體
public class Employee
private int id
private String name
private Contact contact
public class Contact
private String email
private String address
private String zipcode
private String contactTel
public class User
private int id
private String name
private Contact contact
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<component name=
<property name=
<property name=
<property name=
<property name=
</component>
</class>
</hibernate
通常將復合主鍵相關屬性
這個類必須實現 Serializable接口
這個類必須重寫hashCode()和equals()方法
XML
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<composite
<key
<key
</composite
<property name=
<property name=
</class>
</hibernate
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28675.html