一对多表
- 明确表中字段和表的主从关系分析实体类中的属性。
编写实体类
实例: 编写客户实体类和联系人实体类客户与联系人是一对多的关系
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 Customer() {
super();
// TODO Auto-generated constructor stub
}
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;
}
public Set<LinkMan> getLinkMans() {
return linkMans;
}
public void setLinkMans(Set<LinkMan> linkMans) {
this.linkMans = linkMans;
}
@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 + "]";
}
联系人与客户是多对一的关系
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 LinkMan() {
super();
// TODO Auto-generated constructor stub
}
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;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@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 + "]";
}
3 编写客户实体类映射文件
<hibernate-mapping package="com.lanou.bean">
<!--在class中写实体类的映射文件 -->
<!--name:类名 table:表名 -->
<class name="Customer" table="cst_customer">
<!--name:类中属性名 table:表中字段名 -->
<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>
<!--配置表示关系的容器 -->
<!-- name中,填写的是实体类中属性名字
inverse 默认维护外键关系
默认值为 false true不维护
结语:inverse 可以减少hibernate无用操作提高效率
cascade 级联操作减少代码量
save-update
delete
all == save-update+delete
建议如果要使用级联就只用save-update
-->
<set name="linkMans" inverse="true" cascade="delete">
<!--colum对应表外键名 -->
<key column="lkm_cust_id"></key>
<!--class填写的,表示一对多中,多表的类(全类名) -->
<one-to-many class="LinkMan"/>
</set>
</class>
</hibernate-mapping>
4编写联系人的映射文件
<hibernate-mapping package="com.lanou.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_cust_id" column="lkm_cust_id"></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:多对一关系中,对应的实体类的全类名
注意:外键的属性,不要重复配置
-->
<many-to-one name="customer" column="lkm_cust_id" class="Customer"></many-to-one>
</class>
</hibernate-mapping>
5.在hibernate中配置映射路径
<mapping resource="com/lanou/bean/Customer.hbm.xml"/>
<mapping resource="com/lanou/bean/LinkMan.hbm.xml"/>
6.进行添加 修改 删除 测试
添加测试:
public void fun1() {
//获取session
Session session = HibernateUtil.getSession();
//开启事务
Transaction transaction = session.beginTransaction();
//创建客户
Customer customer = new Customer();
customer.setCust_name("wl");
//创建联系人
LinkMan l1 = new LinkMan();
l1.setLkm_name("huhu");
LinkMan l2 = new LinkMan();
l2.setLkm_name("lili");
//添加实体类中关系(把联系人添加到集合中)
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();
}
删除测试
//删除客户的联系人2
@Test
public void fun2() {
//获取session
Session session = HibernateUtil.getSession();
//开启事务
Transaction transaction = session.beginTransaction();
//获取id为1的客户
//持久态 在事务提交时 如果该对象被修改
//那么修改的数据会被同步到数据库
Customer customer = session.get(Customer.class, 1l);
//获取id2的联系人
LinkMan linkMan = session.get(LinkMan.class, 2l);
//处理实体类中关系
customer.getLinkMans().remove(linkMan);
linkMan.setCustomer(null);
//数据库从数据库删除
session.delete(linkMan);
// 提交事务
transaction.commit();
//关闭资源
session.close();
}
修改测试
//为id为1的客户增加一个
@Test
public void fun3() {
//获取session
Session session = HibernateUtil.getSession();
//开启事务
Transaction transaction = session.beginTransaction();
//获取id为1的客户
//持久态 在事务提交时 如果该对象被修改
//那么修改的数据会被同步到数据库
Customer customer = session.get(Customer.class, 1l);
//添加联系人
LinkMan linkMan = new LinkMan();
linkMan.setLkm_name("whh");
//添加到实体类
customer.getLinkMans().add(linkMan);
linkMan.setCustomer(customer);
//保存到数据库
session.save(linkMan);
// 提交事务
transaction.commit();
//关闭资源
session.close();
}
多对多表
1.user测试映射文件
<hibernate-mapping package="com.lanou.bean">
<class name="User" table="sys_user">
<id name="user_id" column="user_id">
<generator class="native"></generator>
</id>
<property name="user_code" column="user_code"></property>
<property name="user_name" column="user_name"></property>
<property name="user_password" column="user_password"></property>
<property name="user_state" column="user_state"></property>
<!-- 表示多对多关系 -->
<!--name=对应容器的名 table:中间表的名字 column:被引用的外键 -->
<!--class:与之对应的表的类名(全类名) column=前面class中被引用的外键 -->
<set name="roles" table="sys_user_role" inverse="true" cascade="save-update">
<key column="user_id"></key>
<many-to-many class="Role" column="role_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
2.角色测试映射文件
<hibernate-mapping package="com.lanou.bean">
<class name="Role" table="sys_role">
<id name="role_id" column="role_id">
<generator class="native"></generator>
</id>
<property name="role_name" column="role_name"></property>
<property name="role_memo" column="role_memo"></property>
<set name="users" table="sys_user_role">
<key column="role_id"></key>
<many-to-many class="User" column="user_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
- 添加 删除测试
添加测试:
//保存两个员工两个角色
//中间表使用的是联合主键如果两张表都维护外键关系也就是都会对中间表进行操作这是会发生重复插入相同的外键值
// 所以多对多操作必须要有一张表放弃对外建的维护
@Test
public void fun1() {
//获取session
Session session = HibernateUtil.getSession();
//开启事务
Transaction transaction = session.beginTransaction();
User user = new User();
user.setUser_name("zl");
Role role = new Role();
role.setRole_memo("包子");
User u = new User();
u.setUser_name("wl");
Role r = new Role();
r.setRole_memo("馒头");
//设置关系
user.getRoles().add(role);
user.getRoles().add(r);
u.getRoles().add(role);
u.getRoles().add(r);
role.getUsers().add(user);
role.getUsers().add(u);
r.getUsers().add(user);
r.getUsers().add(u);
//保存对象到数据库
// session.save(r);
// session.save(role);
// session.save(u);
// session.save(user);
// 提交事务
transaction.commit();
//关闭资源
session.close();
}
删除测试:
@Test
public void fun3() {
//获取session
Session session = HibernateUtil.getSession();
//开启事务
Transaction transaction = session.beginTransaction();
User user = session.get(User.class, 2l);
Role role = session.get(Role.class, 2l);
user.getRoles().remove(role);
role.getUsers().remove(user);
session.save(user);
session.save(role);
// 提交事务
transaction.commit();
//关闭资源
session.close();
}