1.实体类
package com.lanou3g.bean;
public class LinkMan {
private Long lkm_id;
private String lkm_name;
private Long lkm_cust_id;
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 LinkMan() {
super();
}
public LinkMan(Long lkm_id, String lkm_name, Long lkm_cust_id, Character lkm_gender, String lkm_phone,
String lkm_mobile, String lkm_email, String lkm_qq, String lkm_position, String lkm_memo) {
super();
this.lkm_id = lkm_id;
this.lkm_name = lkm_name;
this.lkm_cust_id = lkm_cust_id;
this.lkm_gender = lkm_gender;
this.lkm_phone = lkm_phone;
this.lkm_mobile = lkm_mobile;
this.lkm_email = lkm_email;
this.lkm_qq = lkm_qq;
this.lkm_position = lkm_position;
this.lkm_memo = lkm_memo;
}
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 Long getLkm_cust_id() {
return lkm_cust_id;
}
public void setLkm_cust_id(Long lkm_cust_id) {
this.lkm_cust_id = lkm_cust_id;
}
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;
}
@Override
public String toString() {
return "LinkMan [lkm_id=" + lkm_id + ", lkm_name=" + lkm_name + ", lkm_cust_id=" + lkm_cust_id + ", lkm_gender="
+ lkm_gender + ", lkm_phone=" + lkm_phone + ", lkm_mobile=" + lkm_mobile + ", lkm_email=" + lkm_email
+ ", lkm_qq=" + lkm_qq + ", lkm_position=" + lkm_position + ", lkm_memo=" + lkm_memo + "]";
}
}
package com.lanou3g.bean;
import java.util.HashSet;
import java.util.Set;
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_linkman;
private String cust_phone;
private String cust_mobile;
private Set<LinkMan> linkMans = new HashSet<>();
public Set<LinkMan> getLinkMans() {
return linkMans;
}
public void setLinkMans(Set<LinkMan> linkMans) {
this.linkMans = linkMans;
}
public Customer() {
super();
}
public Customer(Long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,
String cust_linkman, String cust_phone, String cust_mobile) {
super();
this.cust_id = cust_id;
this.cust_name = cust_name;
this.cust_source = cust_source;
this.cust_industry = cust_industry;
this.cust_level = cust_level;
this.cust_linkman = cust_linkman;
this.cust_phone = cust_phone;
this.cust_mobile = cust_mobile;
}
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 + ", cust_source=" + cust_source
+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman
+ ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile + "]";
}
}
2. Customter.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="com.lanou3g.bean">
<class name="LinkMan" table="cst_linkman">
<id name="lkm_id" column="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>
<many-to-one name="customer" column="lkm_cust_id" class="Customer"></many-to-one>
</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="com.lanou3g.bean">
<class name="Customer" table="cst_customer">
<id name="cust_id" column="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_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
<set name="linkMans" cascade="save-update">
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan" />
</set>
</class>
</hibernate-mapping>
3.hibernater测试多对一
package com.lanou3g.bean
import org.hibernate.Session
import org.hibernate.Transaction
import org.junit.Test
public class Demo01 {
@Test
public void fun1() {
// 获取session
Session session = HibernateUtils.getSession()
// 开启事务
Transaction transaction = session.beginTransaction()
// 创建客户
Customer customer = new Customer()
customer.setCust_name("蓝欧科技")
// 创建联系人
LinkMan l1 = new LinkMan()
l1.setLkm_name("杨虎")
LinkMan l2 = new LinkMan()
l2.setLkm_name("刘凯")
// 添加实体类中的关系
// 把联系人添加到集合中
customer.getLinkMans().add(l1)
customer.getLinkMans().add(l2)
// 给联系人设置客户
l1.setCustomer(customer)
l2.setCustomer(customer)
// 保存到数据库
session.save(customer)
session.save(l1)
session.save(l2)
// 提交事务
transaction.commit()
// 关闭资源
session.close()
}
// 为客户id为1的人删除联系人id为2
@Test
public void fun2() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
// 获取id为1的客户
// 持久态 在事务提交的时候 如果该对象被修改了
// 那么修改的数据会被同步到数据中
Customer customer = session.get(Customer.class, 1l)
// 获取id为2的联系人
LinkMan linkMan = session.get(LinkMan.class, 2l)
// 处理实体类中的关系
customer.getLinkMans().remove(linkMan)
linkMan.setCustomer(null)
// 从数据库中删除
session.delete(linkMan)
//session.update(customer)
transaction.commit()
session.close()
}
// 为客户id为1 增加联系人
@Test
public void fun3() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
Customer customer = session.get(Customer.class, 1l)
//创建新的联系人
LinkMan l3 = new LinkMan()
l3.setLkm_name("龙哥")
//处理实体类中的关系
l3.setCustomer(customer)
customer.getLinkMans().add(l3)
//保存到数据库
session.save(l3)
session.update(customer)
transaction.commit()
session.close()
}
//新增客户 和 联系人(李胜运)
//级连操作:可以使你减少几行代码的工作量
@Test
public void fun4() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
Customer customer = new Customer()
customer.setCust_name("丰收科技")
LinkMan linkMan = new LinkMan()
linkMan.setLkm_name("你随意")
//设置实体类
customer.getLinkMans().add(linkMan)
linkMan.setCustomer(customer)
//保存到数据库
session.save(customer)
session.save(linkMan)
transaction.commit()
session.close()
}
//级连 删除id为1客户同时删除联系人
@Test
public void fun5() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
Customer customer = session.get(Customer.class, 1L)
session.delete(customer)
transaction.commit()
session.close()
}
}