Hibernate 多表关联映射- 一对多关系映射(one-to-many)
Hibernage.cfg.xml:
<hibernate-configuration> <session-factory name="sessionFactory"> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="cn/hbm/Person.hbm.xml" /> <mapping resource="cn/hbm/Department.hbm.xml" /> </session-factory> </hibernate-configuration>
Person:
public class Person { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
hbm.xml:
<hibernate-mapping package="cn.model"> <class name="Person" table="PERSON"> <id name="id" column="ID"> <generator class="native"></generator> </id> <property name="name" column="NAME" type="java.lang.String" /> </class> </hibernate-mapping>
Department:
public class Department { private Integer id; private String name; private Set<Person> persons; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons) { this.persons = persons; } }hbm.xml:
<hibernate-mapping package="cn.model"> <class name="Department" table="DEPARTMENT"> <id name="id" column="ID"> <generator class="native"></generator> </id> <property name="name" column="NAME" type="java.lang.String" /> <set name="persons"> <key column="DEPT_ID" /> <one-to-many class="Person" /> </set> </class> </hibernate-mapping>
在Department实体中有一个Set<> 集合用来存储该部门下的所有员工,在hbm.xml 可以使用Set实现映射关系,one-to-many中需要指定Set中存储的对象来自哪个实体;并且通过Key中的列名来进行关连映射操作。
添加数据:
public void save(){ Session session=null; Transaction tran=null; try{ Department dept=new Department(); dept.setName("IT开发部"); Person person1=new Person(); person1.setName("汤姆克路斯"); person1.setDept(dept); Person person2=new Person(); person2.setName("大为史密斯"); person2.setDept(dept); // 或者(结果一样,但是Hibenate的处理流程完全不一样) //Person person1=new Person(); //person1.setDept(dept); //Person person2=new Person(); //person2.setName("大为史密斯"); //Set<Person> sets=new HashSet(); //sets.add(person1); //sets.add(person2); //Department dept=new Department(); //dept.setName("IT开发部"); //dept.setPersons(sets); session=HibernateSessionFactory.getSession(); tran=session.beginTransaction(); session.save(dept); session.save(person1); session.save(person2); tran.commit(); }catch(Exception e){ if(session!=null){ session.close(); } } }
获取部门:
public Department getDepartmentById(Serializable id){ Session session=null; try{ session=HibernateSessionFactory.getSession(); Department dept=(Department)session.get(Department.class, id); //关于延迟加载(lazy)和强制加载(Hibernate.initialize(Object proxy) ) 等 //在配置文件里面可以用lazy=true,在程序里面可以用强制加载的方法Hibernate.initialize(Object proxy) 方法强制加载这样就相当于动态改变为lazy=false。 Hibernate.initialize(dept.getPersons()); return dept; }catch(Exception e){ if(session!=null){ session.close(); } } return null; }
测试:
@Test public void testDeptList(){ Demo demo=new Demo(); Department dept=demo.getDepartmentById(1); System.out.println("部门:"+dept.getName()); System.out.println("员工有:"); for(Person per : dept.getPersons()){ System.out.println(per.getName()); } }
结果 :