@Test public void testGetDepartment() { Department department = (Department) session.get(Department.class, 1); System.out.println(department.getDepartmentName());
}
默认情况下会使用懒加载。如果这里不访问manager的话就不会去查询manager对象。
1 2 3 4 5 6 7 8 9 10 11
Hibernate: select department0_.hb_department_id as hb1_4_0_, department0_.hb_department_name as hb2_4_0_, department0_.hb_manager_id as hb3_4_0_ from atguigu.hb_department department0_ where department0_.hb_department_id=? dept-aa
这里我继续,来查询一些manager对象。
1 2 3 4 5 6
@Test public void testGetDepartment() { Department department = (Department) session.get(Department.class, 1); System.out.println(department.getManager());
Hibernate: select department0_.hb_department_id as hb1_4_0_, department0_.hb_department_name as hb2_4_0_, department0_.hb_manager_id as hb3_4_0_ from atguigu.hb_department department0_ where department0_.hb_department_id=? Hibernate: select manager0_.hb_manager_id as hb1_5_1_, manager0_.hb_manager_name as hb2_5_1_, department1_.hb_department_id as hb1_4_0_, department1_.hb_department_name as hb2_4_0_, department1_.hb_manager_id as hb3_4_0_ from atguigu.hb_manager manager0_ left outer join atguigu.hb_department department1_ on manager0_.hb_manager_id=department1_.hb_department_id where manager0_.hb_manager_id=? com.mamh.hibernate.demo.entities.Manager@64ec96c6 =destroy=
Hibernate: select department0_.hb_department_id as hb1_4_0_, department0_.hb_department_name as hb2_4_0_, department0_.hb_manager_id as hb3_4_0_ from atguigu.hb_department department0_ where department0_.hb_department_id=? Hibernate: select manager0_.hb_manager_id as hb1_5_1_, manager0_.hb_manager_name as hb2_5_1_, department1_.hb_department_id as hb1_4_0_, department1_.hb_department_name as hb2_4_0_, department1_.hb_manager_id as hb3_4_0_ from atguigu.hb_manager manager0_ left outer join atguigu.hb_department department1_ on manager0_.hb_manager_id=department1_.hb_manager_id where manager0_.hb_manager_id=? Hibernate: select department0_.hb_department_id as hb1_4_0_, department0_.hb_department_name as hb2_4_0_, department0_.hb_manager_id as hb3_4_0_ from atguigu.hb_department department0_ where department0_.hb_manager_id=? com.mamh.hibernate.demo.entities.Manager@456d6c1e =destroy=
Hibernate: select manager0_.hb_manager_id as hb1_5_1_, manager0_.hb_manager_name as hb2_5_1_, department1_.hb_department_id as hb1_4_0_, department1_.hb_department_name as hb2_4_0_, department1_.hb_manager_id as hb3_4_0_ from atguigu.hb_manager manager0_ left outer join atguigu.hb_department department1_ on manager0_.hb_manager_id=department1_.hb_manager_id where manager0_.hb_manager_id=? com.mamh.hibernate.demo.entities.Manager@2ca26d77 =destroy=
最后:不要两个表里都使用一个外键来关联另外的一个对象。
#基于主键的一对一关联关系
两张表的主键一样就是基于主键的一对一映射 Department 类和Manager类还是那个类,代码不变
hbm配置映射文件需要改动一下
在department.hbm.xml中使用one to one来映射,同时要使用constrained=”true”属性。
Hibernate: insert into atguigu.hb_manager (hb_manager_name) values (?) =destroy= Hibernate: insert into atguigu.hb_department (hb_department_name, hb_department_id) values (?, ?)
查询操作
查询department
1 2 3 4 5 6
@Test public void testGetDepartment() { Department department = (Department) session.get(Department.class, 1); System.out.println(department.getDepartmentName());
}
1 2 3 4 5 6 7 8 9
Hibernate: select department0_.hb_department_id as hb1_4_0_, department0_.hb_department_name as hb2_4_0_ from atguigu.hb_department department0_ where department0_.hb_department_id=? dept-aaa
Hibernate: select manager0_.hb_manager_id as hb1_5_1_, manager0_.hb_manager_name as hb2_5_1_, department1_.hb_department_id as hb1_4_0_, department1_.hb_department_name as hb2_4_0_ from atguigu.hb_manager manager0_ left outer join atguigu.hb_department department1_ on manager0_.hb_manager_id=department1_.hb_department_id where manager0_.hb_manager_id=? mgr-aaa =destroy=