博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate入门二
阅读量:5055 次
发布时间:2019-06-12

本文共 7194 字,大约阅读时间需要 23 分钟。

  学习一对多的关联关系映射并操作  

一:关系表达

  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 Set
linkmens; 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 + "]"; } }
Customer类
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;    }    }
LinkMan类

四:编写配置文件

LinkMan.hbm.xml
Customer.hbm.xml
com.mysql.jdbc.Driver
jdbc:mysql:///crm_hibernate
root
root
org.hibernate.dialect.MySQLDialect
true
true
update
hibernate.cfg.xml

五:编写测试类

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();    }}
测试类

 

转载于:https://www.cnblogs.com/chiwang/p/9342494.html

你可能感兴趣的文章
How to Create a SharePoint 2010 Project Without SharePoint Server
查看>>
区分块元素和行元素
查看>>
Bypass pattern lock on Sony Xperia Z2 and backup all data
查看>>
又黑我等程序猿
查看>>
AppScan入门工作原理详解
查看>>
夺命雷公狗---node.js---6net模块玩telnet通信(下)
查看>>
夺命雷公狗---linux NO:24 linux下的应用程序编译安装
查看>>
jquery选中radio或checkbox的正确姿势
查看>>
arm 工作模式
查看>>
关于 C# 的 lock
查看>>
EChats+Ajax之柱状图的数据交互
查看>>
DDR DSRAM
查看>>
udp绑定信息
查看>>
C# 数组、ArrayList和List
查看>>
关于 小程序 ios 转时间戳的兼容性 问题
查看>>
python拼接参数不确定的SQL时防注入问题--条件语句最后拼入
查看>>
全文检索引擎Sphinx之初体验
查看>>
JQuery EasyUI combobox动态添加option
查看>>
PHP如何关闭notice级别的错误提示
查看>>
for,forEach,for in ,for of,$.each和$().each应用
查看>>