一对多:(一个客户对应多个联系人)
多的一方维护一的一方的主键作为外键
客户实体:Customer.java
package cn.future.domain; import java.util.HashSet; import java.util.Set; public class Customer { /* * CREATE TABLE `cst_customer` ( `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)', `cust_name` VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)', `cust_source` VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源', `cust_industry` VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业', `cust_level` VARCHAR(32) DEFAULT NULL COMMENT '客户级别', `cust_phone` VARCHAR(64) DEFAULT NULL COMMENT '固定电话', `cust_mobile` VARCHAR(16) DEFAULT NULL COMMENT '移动电话', PRIMARY KEY (`cust_id`) ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; */ private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_phone; private String cust_mobile; //使用set集合表达一对多的关系 private Set<LinkMan> linkMans = new HashSet<LinkMan>(); public Set<LinkMan> getLinkMans() { return linkMans; } public void setLinkMans(Set<LinkMan> linkMans) { this.linkMans = linkMans; } 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_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 + "]"; } }
客户实体对应的映射文件:Customer.hbm.xml
<?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="cn.future.domain" > <class name="Customer" table="cst_customer" > <id name="cust_id" > <generator class="native"></generator> </id> <property name="cust_name" column="cust_name" ></property> <property name="cust_source" column="cust_source" ></property> <property name="cust_industry" column="cust_industry" ></property> <property name="cust_level" column="cust_level" ></property> <property name="cust_phone" column="cust_phone" ></property> <property name="cust_mobile" column="cust_mobile" ></property> <!-- 一对多的关系在配置文件中配置 --> <!-- name属性:集合属性名 column属性:外键列名 class属性:与Costomer关联的对象完整类名 --> <!-- 级联操作:cascade save-update:级联保存更新 delete:级联删除 all: 上面两个相加 级联操作:一般情况下是不配置的,要配置也是配置save-update(企业中一般不用这个属性) --> <!-- inverse属性:配置关系是否维护 true:customer不维护关系 false(默认值):customer维护关系 inverse属性:性能优化,提高关系维护的性能 --> <set name="linkMans"> <key column="lkm_cust_id"></key> <one-to-many class="LinkMan"></one-to-many> </set> </class> </hibernate-mapping>
联系人实体:LinkMan.java
package cn.future.domain; public class LinkMan { /*CREATE TABLE `cst_linkman` ( `lkm_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)', `lkm_name` varchar(16) DEFAULT NULL COMMENT '联系人姓名', `lkm_cust_id` bigint(32) NOT NULL COMMENT '客户id', `lkm_gender` char(1) DEFAULT NULL COMMENT '联系人性别', `lkm_phone` varchar(16) DEFAULT NULL COMMENT '联系人办公电话', `lkm_mobile` varchar(16) DEFAULT NULL COMMENT '联系人手机', `lkm_email` varchar(64) DEFAULT NULL COMMENT '联系人邮箱', `lkm_qq` varchar(16) DEFAULT NULL COMMENT '联系人qq', `lkm_position` varchar(16) DEFAULT NULL COMMENT '联系人职位', `lkm_memo` varchar(512) DEFAULT NULL COMMENT '联系人备注', PRIMARY KEY (`lkm_id`), KEY `FK_cst_linkman_lkm_cust_id` (`lkm_cust_id`), CONSTRAINT `FK_cst_linkman_lkm_cust_id` FOREIGN KEY (`lkm_cust_id`) REFERENCES `cst_customer` (`cust_id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;*/ private Long lkm_id; private String lkm_name; private Character lkm_gender; private String lkm_phone; private String lkm_mobile; private String lkm_email; private String lkm_qq; private String lkm_position; private String lkm_memo; //表达多对一的关系 private Customer customer; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.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_mobile() { return lkm_mobile; } public void setLkm_mobile(String lkm_mobile) { this.lkm_mobile = lkm_mobile; } 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_position() { return lkm_position; } public void setLkm_position(String lkm_position) { this.lkm_position = lkm_position; } public String getLkm_memo() { return lkm_memo; } public void setLkm_memo(String lkm_memo) { this.lkm_memo = lkm_memo; } }
联系人实体对应的映射文件:LinkMan.hbm.xml
<?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="cn.future.domain" > <class name="LinkMan" table="cst_linkman" > <id name="lkm_id" > <generator class="native"></generator> </id> <property name="lkm_name" column="lkm_name" ></property> <property name="lkm_gender" column="lkm_gender" ></property> <property name="lkm_phone" column="lkm_phone" ></property> <property name="lkm_mobile" column="lkm_mobile" ></property> <property name="lkm_email" column="lkm_email" ></property> <property name="lkm_qq" column="lkm_qq" ></property> <property name="lkm_position" column="lkm_position" ></property> <property name="lkm_memo" column="lkm_memo" ></property> <!-- 多对一的关系在配置文件中配置 --> <!-- name属性:引用属性名 column属性:外键列名 class属性:与LinkMan关联的对象完整类名 --> <!-- 级联操作:cascade save-update:级联保存更新 delete:级联删除 all: 上面两个相加 级联操作:一般情况下是不配置的,要配置也是之配置save-update --> <many-to-one name="customer" column="lkm_cust_id" class="Customer"></many-to-one> </class> </hibernate-mapping>
注意:
客户:因为一个客户对应多个联系人,所以在客户数据库表中没有关于联系人的任何字段信息,但是在客户实体类中却维护了联系人的set集合并且在客户的映射文件中也有维护联系人,并注明在联系人表中存放的外键是这个客户的主键,也就是说客户的映射文件自动把客户的cust_id字段对应到联系人表中的lkm_cust_id字段
联系人:联系人实体类中存放了对应客户的整个类对象,但在联系人表中却只维护了对应客户的主键(作为外键),联系人的映射文件中也有对这个外键的描述
实体类和映射文件中紫色的文字为hibernate对于一对多|多对一的描述
操作:
package cn.future.one2many; import java.util.Set; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import cn.future.domain.Customer; import cn.future.domain.LinkMan; import cn.future.utils.HibernateUtils; public class Demo { @Test //一对多|多对一关系操作 public void fun(){ //1获得session Session session = HibernateUtils.openSession(); //2开启事务 Transaction ts = session.beginTransaction(); //3操作 Customer customer = new Customer(); customer.setCust_name("Future"); LinkMan linkMan1 = new LinkMan(); LinkMan linkMan2 = new LinkMan(); linkMan1.setLkm_name("ms"); linkMan2.setLkm_name("grf"); //表达一对多的,客户下有多个联系人 (如果客户映射文件中设置inverse="true")则下面两行代码可以不写 customer.getLinkMans().add(linkMan1); customer.getLinkMans().add(linkMan2); //表达多对一,多个联系人对应一个客户 linkMan1.setCustomer(customer); linkMan2.setCustomer(customer); session.save(customer); session.save(linkMan1); session.save(linkMan2); //4提交事务 ts.commit(); //5关闭资源 session.close(); } @Test //为已知客户增加联系人 public void fun1(){ //1获得session Session session = HibernateUtils.openSession(); //2开启事务 Transaction ts = session.beginTransaction(); //3操作 //获得要操作的客户 Customer customer = session.get(Customer.class, 1l); //创建联系人 LinkMan lk = new LinkMan(); lk.setLkm_name("home"); //把联系人添加到客户,将客户设置到联系人中 customer.getLinkMans().add(lk); lk.setCustomer(customer); //执行保存操作 (只需要保存联系人,因为现在customer对象是持久化状态,可以直接改变属性,当事务提交和资源关闭的时候会自动保存customer) session.save(lk); //4提交事务 ts.commit(); //5关闭资源 session.close(); } @Test //为客户删除联系人(相当于把联系人关联的客户外键置空) public void fun2(){ //1获得session Session session = HibernateUtils.openSession(); //2开启事务 Transaction ts = session.beginTransaction(); //3操作 //获得要操作的客户 Customer customer = session.get(Customer.class, 1l); //获取要移除的联系人 LinkMan lk = session.get(LinkMan.class, 3l); //将联系人从客户列表中移除,同时将联系人对应的客户置空 customer.getLinkMans().remove(lk); lk.setCustomer(null); //4提交事务 ts.commit(); //5关闭资源 session.close(); } @Test //获取客户下联系人信息 public void fun3(){ //1获得session Session session = HibernateUtils.openSession(); //2开启事务 Transaction ts = session.beginTransaction(); //3操作 //获得要操作的客户 Customer customer = session.get(Customer.class, 1l); Set<LinkMan> linkMans = customer.getLinkMans();//直接get联系人列表就可以,不用再根据客户的主键查联系人 for(LinkMan lk : linkMans){ System.out.println(lk.getLkm_name()); } //4提交事务 ts.commit(); //5关闭资源 session.close(); } }
注意:当一的一方维护了inverse这个属性的时候目的如下图(就是少了update执行语句,一的一方给多的一方赋值外简值用的update语句)
<!-- inverse属性:配置关系是否维护 true:customer不维护关系 false(默认值):customer维护关系 inverse属性:性能优化,提高关系维护的性能 --> <set name="linkMans" inverse="true"> <key column="lkm_cust_id"></key> <one-to-many class="LinkMan"></one-to-many> </set>
没有inverse这个属性控制台sql执行情况
加了inverse="true"控制台sql执行情况
如果一的一方不需要查找多的一方的list,那么一的一方的实体类不必维护set集合