【SSH三个框架】Hibernate第七基金会:不少下属业务
相对于上述一关系,在这里,下一个一对多关系说明。
另外,在上述。我们描述了许多人描述的一一对应关系。在关系数据库是多对一的关系,但也有许多关系。但,只知道它是不够的,Hibernate它是一种面向对象的结构,在Hibernate还是多对一的关系。没有一对多,所以我们须要额外加入一对多的关系。
部门实体类:Department.java
package cn.itcast.hibernate.domain; import java.util.Set; public class Department { private int id; private String name; private Set<Employee> emps; public Set<Employee> getEmps() { return emps; } public void setEmps(Set<Employee> emps) { this.emps = emps; } 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; } }
部门映射关系:Department.hbm.xml
<?我们须要注意的是:在部门实体类中,我们添加了一个set集合属性,而且在映射文件里我们也添加了一个set属性,这是为了映射一对多的关系。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="cn.itcast.hibernate.domain"> <class name="Department" table="tb_Department"> <id name="id"> <generator class="native"/> </id> <property name="name" /> <set name="emps"> <key column="depart_id" /> <one-to-many class="Employee" /> </set> </class> </hibernate-mapping>
在映射文件里的set标签。我们定义了一个key标签,这是外键,必须与员工类的外键同样。
package cn.itcast.hibernate.domain; public class Employee { private int id; private String name; private Department depart; 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 Department getDepart() { return depart; } public void setDepart(Department depart) { this.depart = depart; } }
<?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="cn.itcast.hibernate.domain"> <class name="Employee" table="tb_Employee"> <id name="id"> <generator class="native"/> </id> <property name="name" /> <many-to-one name="depart" column="depart_id" /> </class> </hibernate-mapping>
public class Many2One { public static void main(String[] arg){ Department depart = add(); query(depart.getId()); } static Department add(){ Session s = null; Transaction tx = null; try{ Department depart = new Department(); depart.setName("depart name"); Employee emp1 = new Employee(); emp1.setDepart(depart); //建立两个对象的关联关系 emp1.setName("emp name"); Employee emp2 = new Employee(); emp2.setDepart(depart); //建立两个对象的关联关系 emp2.setName("emp name"); s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(depart); s.save(emp1); //emp对象和depart对象保存的先后顺序不同也会有一定的差异 s.save(emp2); //假设员工(emp)先插入,部门(depart)后插入。会生成三条数据库语句。 //由于在两者都插入之后,emp的depart_id字段还未插入,所以还要有最后一条语句插入 tx.commit(); return depart; }finally{ if(s!=null){ s.close(); } } } static Department query(int departId){ Session s = null; Transaction tx = null; try{ s = HibernateUtil.getSession(); tx = s.beginTransaction(); Department depart = (Department)s.get(Department.class, departId); //依据ID查询 System.out.println("emp size:"+depart.getEmps().size()); //Hibernate.initialize(depart.getEmps()); tx.commit(); return depart; }finally{ if(s!=null){ s.close(); } } } }在main功能,我们增加了两个数据,随后的查询部门id。你可以得到两个结果。