多对一单向关联
1)关联映射
把数据库中的表之间(业务)关系映射为对象,在对象之间建立一对多,一对一,多对多等关联。
2)好处(简单,方便)
对由关联关系的多张表之间的操作,转化为对对象之间的操作
3)多对一单向关联:
Emp----->Dept
public class Emp {
// 从Emp------>Dept
private Dept dept;
...
}
<many-to-one name="dept" class="Dept" column="deptId">
public class Emp {
// 从Emp------>Dept
private Dept dept;
...
}
<many-to-one name="dept" class="Dept" column="deptId">
4)orm映射文件的格式 在多的一端配置
<many-to-one name="属性名" class="与之关联的类" column="表中的列名"/>
一对多单向关联
从“一”的一端查询到“多”的一端
1)实体类(po/entity)
“一”的一端用set集合
为什么用set集合:因为不能确定多的一端有多少对象,而且没有顺序要求,使用set集合来保存多的一端的对象维护一对多的关联关系
2)orm映射文件
<set name="emps" >
<!-- deptId维护关联关系,追加到t_emp中
值: 是当前Dept的主键的值-->
<key column="deptId"></key>
<one-to-many class="Emp"/>
</set>
<!-- deptId维护关联关系,追加到t_emp中
值: 是当前Dept的主键的值-->
<key column="deptId"></key>
<one-to-many class="Emp"/>
</set>
多对一双向关联
就是一对多单向关联和多对弈单向关联二者合二为一
注意:那个外键(两个约束必须要执行同一个外键):deptId,多对一 一对多,外键名字必须一致
<many-to-one name="dept" class="Dept" column="deptId">
</many-to-one>
<set name="emps" >
<key column="deptId"></key>
<one-to-many class="Emp"/>
</set>
</many-to-one>
<set name="emps" >
<key column="deptId"></key>
<one-to-many class="Emp"/>
</set>
多对多单向关联
Job.java
public class Job {
private int id;
private String jname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getJname() {
return jname;
}
public void setJname(String jname) {
this.jname = jname;
}
}
Emp.java
public class Emp {
private int id;
private String ename;
private Set jobs;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Set getJobs() {
return jobs;
}
public void setJobs(Set jobs) {
this.jobs = jobs;
}
}
Job.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
package="com.tarena.po">
<class name="Job" table="t_job">
<id name="id">
<generator class="native"></generator>
</id>
<property name="jname"></property>
</class>
</hibernate-mapping>
Emp.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
package="po">
<class name="Emp" table="t_emp">
<id name="id">
<generator class="native"></generator>
</id>
<property name="ename"></property>
<!--table="t_emp_job" 创建一个新的表,来维护关联关系 -->
<set name="jobs" table="t_emp_job">
<!-- key的值是当前主键的值 -->
<key column="eid"></key>
<!-- jid的值是job中默认的主键的值 -->
<many-to-many class="Job" column="jid">
</many-to-many>
</set>
</class>
</hibernate-mapping>
多对多双向关联
Job.java
public class Job {
private int id;
private String jname;
private Set emps;
public Set getEmps() {
return emps;
}
public void setEmps(Set emps) {
this.emps = emps;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getJname() {
return jname;
}
public void setJname(String jname) {
this.jname = jname;
}
}
Emp.java
public class Emp {
private int id;
private String ename;
private Set jobs;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Set getJobs() {
return jobs;
}
public void setJobs(Set jobs) {
this.jobs = jobs;
}
}
Job.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
package="com.tarena.po">
<class name="Job" table="t_job">
<id name="id">
<generator class="native"></generator>
</id>
<property name="jname"></property>
<set name="emps" table="t_emp_job">
<key column="jid"></key>
<many-to-many class="Emp" column="eid"></many-to-many>
</set>
</class>
</hibernate-mapping>
Emp.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
package="com.tarena.po">
<class name="Emp" table="t_emp">
<id name="id">
<generator class="native"></generator>
</id>
<property name="ename"></property>
<!--table="t_emp_job" 创建一个新的表,来维护关联关系 -->
<set name="jobs" table="t_emp_job">
<!-- key的值是当前主键的值 -->
<key column="eid"></key>
<!-- jid的值是job中默认的主键的值 -->
<many-to-many class="Job" column="jid">
</many-to-many>
</set>
</class>
</hibernate-mapping>
一对一主键双向关联
Card.java
public class Card {
private int id;
private String cardNo;
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
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;
}
}
Person.java
public class Person {
private int id;
private String pname;
private Card card;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
}
Card.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
package="com.tarena.po">
<class name="Card" table="t_card">
<id name="id">
<generator class="native"></generator>
</id>
<property name="cardNo"></property>
<one-to-one name="person" class="Person"></one-to-one>
</class>
</hibernate-mapping>
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
package="com.tarena.po">
<class name="Person" table="t_person">
<id name="id">
<generator class="foreign">
<!-- 参照Card的id属性(主键) -->
<param name="property">card</param>
</generator>
</id>
<property name="pname"></property>
<one-to-one name="card" class="Card" constrained="true" ></one-to-one>
</class>
</hibernate-mapping>
一对一唯一外键双向关联
User.java
public class User {
private int id;
private String uname;
private int age;
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address.java
public class Address {
private int id;
private String province;
private String city;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
User.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
package="com.tarena.po">
<class name="User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="uname"></property>
<property name="age"></property>
<many-to-one name="address" column="addressId"
unique="true"></many-to-one>
</class>
</hibernate-mapping>
Address.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
package="com.tarena.po">
<class name="Address" table="t_address">
<id name="id">
<generator class="native"></generator>
</id>
<property name="province"></property>
<property name="city"></property>
<one-to-one name="user" class="User"
property-ref="address" ></one-to-one>
</class>
</hibernate-mapping>