Hibernate—第一季who are you 实体关系:一个Department对象对应多个User对象。 首先我们创建实体对象和相应的xml文件。 private Long user_id; private String user_name; private String user_pass; private Department department; public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public Long getUser_id() { return user_id; } public void setUser_id(Long user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_pass() { return user_pass; } public void setUser_pass(String user_pass) { this.user_pass = user_pass; }
注意 many-to-one 部分(User实体中有个department属性对应user表中的user_dpt_id这个外键,它来自Department实体) <hibernate-mapping package='com.domain'> <class name='User' table='user'> <id name='user_id' column='user_id'> <generator class='native'>generator> id> <property name='user_name' column='user_name'>property> <property name='user_pass' column='user_pass'>property> <many-to-one name='department' column='user_dpt_id' class='Department'>many-to-one> class> hibernate-mapping>
注意 set集合 private Long dpt_id; private String dpt_name; private Set users = new HashSet(); public Set getUsers() { return users; } public void setUsers(Set users) { this.users = users; } public Long getDpt_id() { return dpt_id; } public void setDpt_id(Long dpt_id) { this.dpt_id = dpt_id; } public String getDpt_name() { return dpt_name; } public void setDpt_name(String dpt_name) { this.dpt_name = dpt_name; }
注意 set 部分(Department实体中有一个users集合来自User实体。外键user_dpt_id) <hibernate-mapping package='com.domain'> <class name='Department' table='department'> <id name='dpt_id' column='dpt_id'> <generator class='native'>generator> id> <property name='dpt_name' column='dpt_name'>property> <set name='users'> <key column='user_dpt_id'>key> <one-to-many class='User'/> set> class> hibernate-mapping>
测试代码如下: User u = new User(); u.setUser_name('哈哈1'); Department dpt = new Department(); dpt.setDpt_name('保洁'); dpt.getUsers().add(u); session.save(u); session.save(dpt);
如果保存对象过多这样就特别麻烦。这就引出我们另外一个操作级联操作:cascade属性值有三个 save-update: 级联保存更新(一般只用这个) delete:级联删除(没把握轻易别用,不然你们项目经理会弄死你) all:save-update+delete 这时将Department.hbm.xml中set加上cascade 'users' cascade='save-update'> <key column='user_dpt_id'>key> class='User'/> set>
测试代码如下: User u = new User(); u.setUser_name('哈哈'); User u1 = new User(); u1.setUser_name('呵呵'); Department dpt = new Department(); dpt.setDpt_name('保洁'); dpt.getUsers().add(u); dpt.getUsers().add(u1); session.save(dpt);
理解为:保存dpt时也将dpt中set集合内的所有user也一并保存了。 细心的小伙伴可能发现控制台的输出,怎么会有update出现。那是因为User会维护外键user_dpt_id。而Department也会维护外键user_dpt_id。这样多次访问数据库会增加数据库压力。所以必须有一方放弃维护外键。 一对多关系中:一的一方放弃,也只能一的一方放弃。多的一方不能放弃。(毕竟user_dpt_id是在User表中) <set name='users' cascade='save-update' inverse='true'> <key column='user_dpt_id'>key> class='User'/> set>
inverse默认为false表示维护外键,修改为true表示放弃维护外键。
实体关系:多个Department对象对应多个User对象。(一人身兼多职) 首先我们创建实体对象和相应的xml文件。 private Long user_id; private String user_name; private String user_pass; private Set departments = new HashSet();
public Set getDepartments() { return departments; } public void setDepartments(Set departments) { this.departments = departments; } public Long getUser_id() { return user_id; } public void setUser_id(Long user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_pass() { return user_pass; } public void setUser_pass(String user_pass) { this.user_pass = user_pass; }
注意 set部分(User实体中有departments集合来自Department实体。中间表为user_dpt,User提供外键为user_id,Department提供外键为dpt_id。并且使用级联保存更新) <hibernate-mapping package='com.domain'> <class name='User' table='user'> <id name='user_id' column='user_id'> <generator class='native'>generator> id> <property name='user_name' column='user_name'>property> <property name='user_pass' column='user_pass'>property> <set name='departments' table='user_dpt' cascade='save-update'> <key column='user_id'>key> <many-to-many class='Department' column='dpt_id'>many-to-many> set> class> hibernate-mapping>
private Long dpt_id; private String dpt_name; private Set users = new HashSet(); public Set getUsers() { return users; } public void setUsers(Set users) { this.users = users; } public Long getDpt_id() { return dpt_id; } public void setDpt_id(Long dpt_id) { this.dpt_id = dpt_id; } public String getDpt_name() { return dpt_name; } public void setDpt_name(String dpt_name) { this.dpt_name = dpt_name; }
注意 set部分(Department实体中有users集合来自User实体。中间表为user_dpt,Department提供外键为dpt_id,User提供外键为user_id。没有使用级联保存更新,但放弃维护外键) <hibernate-mapping package='com.domain'> <class name='Department' table='department'> <id name='dpt_id' column='dpt_id'> <generator class='native'>generator> id> <property name='dpt_name' column='dpt_name'>property> <set name='users' table='user_dpt' inverse='true'> <key column='dpt_id'>key> <many-to-many class='User' column='user_id'/> set> class> hibernate-mapping>
测试代码如下: Department dpt = new Department(); dpt.setDpt_name('保洁');
Department dpt1 = new Department(); dpt1.setDpt_name('保安');
User u = new User(); u.setUser_name('哈哈'); u.getDepartments().add(dpt); u.getDepartments().add(dpt1);
User u1 = new User(); u1.setUser_name('呵呵'); u1.getDepartments().add(dpt); u1.getDepartments().add(dpt1);
session.save(u); session.save(u1);
结论: 将来在开发中,如果遇到多对多关系。一定要选择一方放弃维护关系,一般谁来放弃要看业务方向。 例如录入员工时,需要为员工指定所属角色。业务方向就是由员工维护角色,角色不需要维护与员工关系,角色放弃维护。
更多内容请(⊙o⊙)… ....................END.......................
|