hibernate的基础学习--一对多关联
基本的用户和部门类,只有uuid和名称,没有其余字段。
配置文件
部门:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <!-- 映射配置 --> 6 <hibernate-mapping> 7 <class name="hib.po.Dept" table="dept" schema="hibernatedb"> 8 <!-- 类的唯一标示和表中的主键映射 --> 9 <id name="deptId" column="dept_id"> 10 <!-- 主键生成策略:native(mysql自增) --> 11 <generator class="uuid"></generator> 12 </id> 13 14 <property name="deptName" column="deptname"></property> 15 16 <!-- 关系字段:用户 one2many 17 name:关联关系属性 18 class:关联关系属性的类型 19 column:指定生成表中的关系字段名称(外键名) 20 inverse="true":放弃维护关系功能 21 --> 22 <set name="users" inverse="false"> 23 <!-- column: 用来对应 关联表(t_user)中的外键名称 --> 24 <key column="dept_id"></key> 25 <!-- class:关联关系属性(集合)中的元素的类型 --> 26 <one-to-many class="hib.po.User"/> 27 </set> 28 </class> 29 </hibernate-mapping>
用户:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <!-- 映射配置 --> 6 <hibernate-mapping> 7 <class name="hib.po.User" table="user" schema="hibernatedb"> 8 <!-- 类的唯一标示和表中的主键映射 --> 9 <id name="userId" column="user_id"> 10 <!-- 主键生成策略:native(mysql自增) --> 11 <generator class="uuid"></generator> 12 </id> 13 14 <!-- 普通属性和普通字段映射 --> 15 <property name="username" column="username"></property> 16 17 <!-- 关系字段:部门 many2one 18 name:关联关系属性 19 class:关联关系属性的类型 20 column:指定生成表中的关系字段名称(外键名) 21 --> 22 <many-to-one 23 name="dept" 24 class="hib.po.Dept" 25 column="dept_id" 26 ></many-to-one> 27 </class> 28 </hibernate-mapping>
基础的增删改查方法
1 /** 2 * @author nunu 3 * 测试hibernate OneToMany 4 */ 5 public class TestHibernateOneToMany { 6 7 /** 8 * 添加用户 9 */ 10 @Test 11 public void addUser() { 12 SessionFactory sf = H3Util.getSessionFactory(); 13 Session session = sf.openSession(); 14 Transaction ts = session.beginTransaction(); 15 User s1 = new User(null, "小小", null); 16 User s2 = new User(null, "小五", null); 17 session.save(s1); 18 session.save(s2); 19 20 ts.commit(); 21 session.close(); 22 } 23 24 /** 25 * 添加部门 26 */ 27 @Test 28 public void addDept() { 29 SessionFactory sf = H3Util.getSessionFactory(); 30 Session session = sf.openSession(); 31 Transaction ts = session.beginTransaction(); 32 Dept dept1 = new Dept(null, "研发部", null); 33 Dept dept2 = new Dept(null, "人事部", null); 34 Dept dept3 = new Dept(null, "财务部", null); 35 session.save(dept1); 36 session.save(dept2); 37 session.save(dept3); 38 39 ts.commit(); 40 session.close(); 41 } 42 43 /** 44 * 从员工端维护关系:多端维护 45 */ 46 @Test 47 public void updateMany(){ 48 SessionFactory sf = H3Util.getSessionFactory(); 49 Session session = sf.openSession(); 50 Transaction ts = session.beginTransaction(); 51 52 User user1 = (User) session.get(User.class, "ff80808157c82de80157c82deb2d0000"); 53 Dept dept1 = (Dept) session.get(Dept.class, "ff80808157c831050157c83106b10000"); 54 System.out.println(dept1.getDeptName()); 55 //从用户角度维护关系:将部门对象添加到用户中 56 user1.setDept(dept1); 57 58 //最后加上 59 session.update(user1); 60 61 ts.commit(); 62 session.close(); 63 System.out.println("===="); 64 } 65 66 /** 67 * 从一端维护关系 68 */ 69 @Test 70 public void updateOne() { 71 SessionFactory sf = H3Util.getSessionFactory(); 72 Session session = sf.openSession(); 73 Transaction ts = session.beginTransaction(); 74 75 User user1 = (User) session.get(User.class, "ff80808157c831830157c83184a90000"); 76 Dept dept1 = (Dept) session.get(Dept.class, "ff80808157c831050157c83106c40001"); 77 System.out.println(dept1.getDeptName()); 78 //从部门角度维护关系:将用户对象添加到部门中 79 Set<User> users = dept1.getUsers(); 80 users.add(user1); 81 82 dept1.setUsers(users); 83 84 session.update(dept1); 85 86 ts.commit(); 87 session.close(); 88 System.out.println("===="); 89 } 90 91 }