一对多
一对多的实体 多对一的实体
需要使用set集合用于保存对象 需要一对多实体的对象
set<对象> 变量名 对象 变量名
package domain; import java.util.HashSet; import java.util.Set; public class Customer { public Set<LinkMan> getLinkMans() { return linkMans; } public void setLinkMans(Set<LinkMan> linkMans) { this.linkMans = linkMans; } private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; private Set<LinkMan> linkMans = new HashSet<LinkMan>(); public Customer() { super(); } public Customer(String cust_name,Long cust_id) { super(); this.cust_id = cust_id; this.cust_name=cust_name; } public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } @Override public String toString() { return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]"; } }
package domain; public class LinkMan { @Override public String toString() { return "LinkMan [lkm_id=" + lkm_id + ", lkm_name=" + lkm_name + ", lkm_gender=" + lkm_gender + ", lkm_phone=" + lkm_phone + ", lkm_email=" + lkm_email + ", lkm_qq=" + lkm_qq + ", lkm_mobile=" + lkm_mobile + ", lkm_memo=" + lkm_memo + ", lkm_postion=" + lkm_postion + ", customer=" + customer + "]"; } private Long lkm_id; private String lkm_name; private Character lkm_gender; private String lkm_phone; private String lkm_email; private String lkm_qq; private String lkm_mobile; private String lkm_memo; private String lkm_postion; private Customer customer; public Long getLkm_id() { return lkm_id; } public void setLkm_id(Long lkm_id) { this.lkm_id = lkm_id; } public String getLkm_name() { return lkm_name; } public void setLkm_name(String lkm_name) { this.lkm_name = lkm_name; } public Character getLkm_gender() { return lkm_gender; } public void setLkm_gender(Character lkm_gender) { this.lkm_gender = lkm_gender; } public String getLkm_phone() { return lkm_phone; } public void setLkm_phone(String lkm_phone) { this.lkm_phone = lkm_phone; } public String getLkm_email() { return lkm_email; } public void setLkm_email(String lkm_email) { this.lkm_email = lkm_email; } public String getLkm_qq() { return lkm_qq; } public void setLkm_qq(String lkm_qq) { this.lkm_qq = lkm_qq; } public String getLkm_mobile() { return lkm_mobile; } public void setLkm_mobile(String lkm_mobile) { this.lkm_mobile = lkm_mobile; } public String getLkm_memo() { return lkm_memo; } public void setLkm_memo(String lkm_memo) { this.lkm_memo = lkm_memo; } public String getLkm_postion() { return lkm_postion; } public void setLkm_postion(String lkm_postion) { this.lkm_postion = lkm_postion; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
orm元数据的表达就是
<!-- 集合,一对多关系,在配置文件中配置 -->
<!--
name属性:集合属性名
column属性: 外键列名
class属性: 与我关联的对象完整类名
-->
<set name="linkMans" >
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan"/>
</set>
多对一
<many-to-one name="customer" column="lkm_cust_id" class="Customer" ></many-to-one>
配置号xml后即可开始操作
public void func1() { Session sesion = HibernateUtils.openSession(); Transaction tx = sesion.beginTransaction(); //-------------------------------------- Customer c = new Customer(); c.setCust_name("as"); LinkMan linkman = new LinkMan(); linkman.setLkm_name("sss"); LinkMan linkman2 = new LinkMan(); linkman2.setLkm_name("ccc"); //一对多 c.getLinkMans().add(linkman); c.getLinkMans().add(linkman2); //多对一 linkman.setCustomer(c); linkman2.setCustomer(c); sesion.save(c);
/*设置级联 不需要下面两行代码*/ sesion.save(linkman); sesion.save(linkman2); //-------------------------------------- tx.commit(); sesion.close(); }
这时候可以设置级联操作 就可以只写一边save了、 cascade属性
//一对多
<!-- 级联操作: cascade save-update: 级联保存更新 delete:级联删除 all:save-update+delete 级联操作: 简化操作.目的就是为了少些两行代码. --> <set name="linkMans" cascade="save-update" inverse="true" lazy="true" fetch="select"> <key column="lkm_cust_id"></key> <one-to-many class="LinkMan"/> </set>
//多对一
<many-to-one name="customer" column="lkm_cust_id" class="Customer" cascade="save-update"></many-to-one>
保存时.两方都会维护外键关系.关系维护两次,重复了.
显然只需要一端在维护关系
//一对多
<!-- inverse属性: 配置关系是否维护. true: 放弃维护关系 false(默认值): 维护关系 inverse属性: 性能优化.提高关系维护的性能. 原则: 无论怎么放弃,总有一方必须要维护关系. 一对多关系中: 一的一方放弃.也只能一的一方放弃.多的一方不能放弃. -->
<set name="linkMans" cascade="save-update" inverse="true" lazy="true" fetch="select"> <key column="lkm_cust_id"></key> <one-to-many class="LinkMan"/> </set>
//多对一
<many-to-one name="customer" column="lkm_cust_id" class="Customer" cascade="save-update"></many-to-one>
多对多
实体1 实体2
需要使用set集合用于保存对象 需要使用set集合用于保存对象
set<对象> 变量名 set<对象> 变量名
package domain; import java.util.HashSet; import java.util.Set; public class User { @Override public String toString() { return "User [user_id=" + user_id + ", user_code=" + user_code + ", user_name=" + user_name + ", user_password=" + user_password + ", user_state=" + user_state + ", roles=" + roles + "]"; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } private Long user_id; private String user_code; private String user_name; private String user_password; private Character user_state; private Set<Role> roles = new HashSet<Role>(); public Long getUser_id() { return user_id; } public void setUser_id(Long user_id) { this.user_id = user_id; } public String getUser_code() { return user_code; } public void setUser_code(String user_code) { this.user_code = user_code; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } public Character getUser_state() { return user_state; } public void setUser_state(Character user_state) { this.user_state = user_state; } }
package domain; import java.util.HashSet; import java.util.Set; public class Role { public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } public Long getRole_id() { return role_id; } public void setRole_id(Long role_id) { this.role_id = role_id; } public String getRole_name() { return role_name; } public void setRole_name(String role_name) { this.role_name = role_name; } public String getRole_memo() { return role_memo; } public void setRole_memo(String role_memo) { this.role_memo = role_memo; } private Long role_id; private String role_name; private String role_memo; private Set<User> users = new HashSet<User>(); }
orm元数据
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain" > <class name="Role" table="sys_role" > <id name="role_id" > <!-- generator:主键生成策略(明天讲) --> <generator class="native"></generator> </id> <property name="role_name"></property> <property name="role_memo"></property> <!-- 使用inverse属性 true: 放弃维护外键关系 false(默认值):维护关系 结论: 将来在开发中,如果遇到多对多关系.一定要选择一方放弃维护关系. 一般谁来放弃要看业务方向. 例如录入员工时,需要为员工指定所属角色. 那么业务方向就是由员工维护角色. 角色不需要维护与员工关系.角色放弃维护 --> <set name="users" table="sys_user_role" inverse="true"> <key column="role_id"></key> <many-to-many class="User" column="user_id"></many-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain" > <class name="User" table="sys_user" > <id name="user_id" > <generator class="native"></generator> </id> <property name="user_name" ></property> <property name="user_code" ></property> <property name="user_password"></property> <property name="user_state"></property> <!-- 多对多描述 --> <!-- 多对多关系表达 --> <!-- name: 集合属性名 table: 配置中间表名 key |-column:外键,别人引用"我"的外键列名 class: 我与哪个类是多对多关系 column:外键.我引用比人的外键列名 --> <!-- cascade级联操作: save-update: 级联保存更新 delete:级联删除 all:级联保存更新+级联删除 结论: cascade简化代码书写.该属性使不使用无所谓. 建议要用只用save-update. 如果使用delete操作太过危险.尤其在多对多中.不建议使用. --> <set name="roles" table="sys_user_role" cascade="save-update"> <key column="user_id"></key> <many-to-many class="Role" column="role_id"></many-to-many> </set> </class> </hibernate-mapping>
操作多对多
public class demo3 { public void function1() { Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //-------------------------------------- User user1 = new User(); User user2 = new User(); user1.setUser_name("user1"); user2.setUser_name("user2"); Role role1 = new Role(); Role role2 = new Role(); role1.setRole_name("role1"); role2.setRole_name("role2"); user1.getRoles().add(role1); user1.getRoles().add(role2); user2.getRoles().add(role1); user2.getRoles().add(role2); role1.getUsers().add(user1); role1.getUsers().add(user2); role2.getUsers().add(user1); role2.getUsers().add(user2); session.save(user1); session.save(user2); session.save(role1); session.save(role2); //-------------------------------------- tx.commit(); session.close(); }
@Test
public void function3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//--------------------------------------
//删除关系
User user = session.get(User.class, 1l);
Role role1 = session.get(Role.class, 1l);
Role role2 = session.get(Role.class, 2l);
Role role3 = session.get(Role.class, 3l);
user.getRoles().remove(role1);
user.getRoles().remove(role2);
user.getRoles().remove(role3);
//session.save(user);
//--------------------------------------
tx.commit();
session.close();
}
}
多对多 使用inverse属性 只允许一方维护关系
原文:https://www.cnblogs.com/hywhyme/p/11628899.html