对相关联映射
一对多关系映射:
<class name= entity.User table= t_user" schema=" hibernate" >
<property name="userName" column="username"/>
<property name="Age" column="age"/>
<property name="Gender" column= "gender" />
<set name=" scores" inverse="true" >
<key>
<column name="uid" not-null= "true" />
</key>
<one-to-many not-found="ignore" class="entity.User/>
</set>
<class name= entity.User table= t_score" schema=" hibernate" >
<property name="sName" column="sname"/>
<property name="Score" column="score"/>
<many-to-one name="user" class= entity.User>
<column name= "uid" not-null= "true" />
</many-to-one>
结果:
一对一
public class Person {
//class’s properties
private int id;
private String name;
private Card card;
//getter and setter method
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Card {
//class properties
private int id;
private String cardNo;
//getter and setter method
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
}
<class name="com.hibernate.Card" table="t_card">
<id name="id">
<generator class="native"></generator>
</id>
<property name="cardNo"></property>
<one-to-one name="person" property-ref="card"></one-to-one>
</class>
**多对多** public class User { //具有的属性 private int id; private String name;
private Set roles;
//getter和setter方法
public Set getRoles() {
return roles;
}
public void setRoles(Set roles) {
this.roles = roles;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Role {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
User.hbm.xml映射文件:
<set name="roles" table="t_user_role">
<key column="user_id"></key>
<many-to-many class="com.bjpowernode.hibernate.Role" column="role_id"/>
</set>
</class>
Role.hbm.xml映射文件:
</class>
配置文件:
</class>