学习一对多的关联关系映射并操作
一:关系表达
1.表中的表达
2.实体中的表达
二:导包
三:创建实体类
package cn.itcast.domain;import java.util.Set;/*** @author 作者* @version 创建时间:2018年7月19日 下午3:38:12* 类说明:*/public class Customer { 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; private Setlinkmens; public Set getLinkmens() { return linkmens; } public void setLinkmens(Set linkmens) { this.linkmens = linkmens; } 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 + ", cust_source=" + cust_source + ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile + "]"; } }
package cn.itcast.domain;/*** @author 作者* @version 创建时间:2018年7月20日 下午3:38:58* 类说明:*/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 Character lkm_gender; private String lkm_name; 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 Long getLkm_id() { return lkm_id; } public void setLkm_id(Long lkm_id) { this.lkm_id = lkm_id; } public Character getLkm_gender() { return lkm_gender; } public void setLkm_gender(Character lkm_gender) { this.lkm_gender = lkm_gender; } public String getLkm_name() { return lkm_name; } public void setLkm_name(String lkm_name) { this.lkm_name = lkm_name; } 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; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
四:编写配置文件
com.mysql.jdbc.Driver jdbc:mysql:///crm_hibernate root root org.hibernate.dialect.MySQLDialect true true update
五:编写测试类
package cn.itcast.one2many;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import cn.itcast.domain.Customer;import cn.itcast.domain.LinkMan;import cn.itcast.utils.HibernateUtils;/*** @author 作者* @version 创建时间:2018年7月20日 下午4:28:54* 类说明:*/public class TestRelationMap { @Test public void fun1() { //1.获取session Session session = HibernateUtils.openSession(); //2.开启事务 Transaction tx = session.beginTransaction(); //3.操作 Customer customer = new Customer(); LinkMan linkman1 = new LinkMan(); LinkMan linkman2 = new LinkMan(); customer.setCust_name("XX公司"); linkman1.setLkm_name("张三"); linkman1.setLkm_name("李四"); customer.getLinkmens().add(linkman1); customer.getLinkmens().add(linkman2); linkman1.setCustomer(customer); linkman2.setCustomer(customer); session.save(customer); session.save(linkman1); session.save(linkman2); //4.提交事务 关闭资源 tx.commit(); session.close(); }}