在软件开发中,类与类之间最普遍的关系就是关联关系,而且关联是有方向的。
通过类图展示的方式讲解类与类之间的关联关系,类与类之间存在着单向关联,也存在着双向关联。如下图所示:
多对一关联和一对多双向关联:
例子:
以员工类(Emp)和部门类(Dept)为例
1.先建立两个实体类
package cn.entity; public class Emp { private Integer empNo; private String empName; private Dept dept; public Integer getEmpNo() { return empNo; } public void setEmpNo(Integer empNo) { this.empNo = empNo; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public Emp(Integer empNo, String empName, Dept dept) { super(); this.empNo = empNo; this.empName = empName; this.dept = dept; } public Emp() { super(); // TODO Auto-generated constructor stub } }
package cn.entity; import java.util.HashSet; import java.util.Set; public class Dept { private Integer deptNo; private String deptName; private Set<Emp> emp=new HashSet<Emp>(); public Set<Emp> getEmp() { return emp; } public void setEmp(Set<Emp> emp) { this.emp = emp; } public Integer getDeptNo() { return deptNo; } public void setDeptNo(Integer deptNo) { this.deptNo = deptNo; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public Dept(Integer deptNo, String deptName) { super(); this.deptNo = deptNo; this.deptName = deptName; } public Dept() { super(); // TODO Auto-generated constructor stub } }
2.书写小配置文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.entity"> <class name="Emp" table="EMP"> <id name="empNo"> <generator class="native"/> </id> <!-- assigned 程序员赋值 native后台DB赋值--> <property name="empName" /> <many-to-one name="dept" class="Dept" column="deptid"></many-to-one> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.entity"> <class name="Dept" table="DEPT"> <id name="deptNo"> <generator class="native"/> </id> <!-- assigned 程序员赋值 native后台DB赋值--> <property name="deptName" /> <set name="emp" cascade="save-update" inverse="true"><!-- casecade级联:取值 : none save-update delete all --> <!-- inverse 默认值 false 主动方 维护关联关系 --> <key column="deptid"></key><!-- key:这里是多的一方定义的外键 --> <one-to-many class="Emp"/><!-- 多的一方的实体类型 --> </set> </class> </hibernate-mapping>
3.大配置关联小配置
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings 数据库连接配置--> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">wth</property> <property name="connection.password">1509184562</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect 方言--> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Echo all executed SQL to stdout 在控制台打印后台sql语句--> <property name="show_sql">true</property> <!-- 格式化语句 --> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- 关联小配置 --> <mapping resource="cn/entity/Emp.hbm.xml" /> <mapping resource="cn/entity/Dept.hbm.xml" /> </session-factory> </hibernate-configuration>
4.书写测试类(使用单元测试方法)
package cn.happy; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import cn.entity.Dept; import cn.entity.Emp; import cn.entity.HibernateUtil; public class OneToMany { //多对一关联 @Test public void OneToManyTewst(){ Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); Emp emp=new Emp(); Dept dept=new Dept(); emp.setEmpName("小河"); emp.setDept(dept); dept.setDeptName("保安部"); session.save(emp); session.save(dept); tx.commit(); HibernateUtil.closeSession(); } //一对多双向关联 //主要是set集合 @Test public void ManyToOneTest(){ Session session = HibernateUtil.currentSession(); List<Dept> dept=session.createQuery("from Dept").list(); for (Dept item : dept) { System.out.println(item.getDeptName()); System.out.println("*******************"); for (Emp emp : item.getEmp()) { System.out.println(emp.getEmpName()); } } } }
今天就到这里,下次见!!!