Hibernate框架双向一对多配置,以及级联操作
程序员文章站
2022-04-24 14:24:24
...
一对多:
一的一方java文件
package bean;
import java.util.HashSet;
import java.util.Set;
public class Custmoer {
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<Linkman> linkmans=new HashSet<Linkman>();
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;
}
public Set<Linkman> getLinkmans() {
return linkmans;
}
public void setLinkmans(Set<Linkman> linkmans) {
this.linkmans = linkmans;
}
}
一的一方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>
<class name="bean.Custmoer" table="cus_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_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
<!-- 配置一对多的关系 集合(linkmans)-->
<!--
name:集合的名字
inverse:true 指定放弃外键的维护权
cascade: save-update 级联保存
delete:级联删除
lazy: true 默认值 默认使用延迟加载
false 不使用延迟加载 立即加载
-->
<set name="linkmans" inverse="true" cascade="save-update,delete">
<key column="wj_id"></key>
<one-to-many class="bean.Linkman"/>
</set>
</class>
</hibernate-mapping>
多的一方java文件
package bean;
public class Linkman {
private Long lkm_id;//'联系人编号(主键)',
private String lkm_name;//'联系人姓名',
private String lkm_gender;// '联系人性别',
private String lkm_phone;// '联系人办公电话',
private String lkm_mobile;// '联系人手机',
private String lkm_email;// '联系人邮箱',
private String lkm_position;// '联系人职位',
private String lkm_memo;// '联系人备注',
//外键
private Custmoer custmoer;
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 String getLkm_gender() {
return lkm_gender;
}
public void setLkm_gender(String 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_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 Custmoer getCustmoer() {
return custmoer;
}
public void setCustmoer(Custmoer custmoer) {
this.custmoer = custmoer;
}
}
多的一方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>
<class name="bean.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_position" column="lkm_position"></property>
<property name="lkm_memo" column="lkm_memo"></property>
<!-- 配置一对多的关系 对象(customer) -->
<!--
name:在自己里面一的一方对象的属性名
class: 一的一方的全限定名
column: 指定外键字段名
-->
<many-to-one name="custmoer" class="bean.Custmoer" column="wj_id"></many-to-one>
</class>
</hibernate-mapping>
级联操作
//级联删除数据
private static void deleteJilian() {
Session session = HibernateUtils.openSession();
Transaction tr =session.beginTransaction();
Custmoer custmoer =session.get(Custmoer.class,1L);
session.delete(custmoer);
tr.commit();
session.close();
}
//级联保存数据
//<set name="linkmans" cascade="save-update,delete">
private static void savaJilian() {
Session session = HibernateUtils.openSession();
Transaction tr =session.beginTransaction();
Custmoer custmoer = new Custmoer();
custmoer.setCust_name("老马");
Linkman linkman = new Linkman();
linkman.setLkm_name("小蜜");
Linkman linkman2=new Linkman();
linkman2.setLkm_name("中密");
Linkman linkman3=new Linkman();
linkman3.setLkm_name("大蜜");
//让客户保存联系人
custmoer.getLinkmans().add(linkman);
custmoer.getLinkmans().add(linkman2);
custmoer.getLinkmans().add(linkman3);
//让联系人关联客户
linkman.setCustmoer(custmoer);
linkman2.setCustmoer(custmoer);
linkman3.setCustmoer(custmoer);
//保存客户
//在xml中关联了linkman表
//<set name="linkmans" cascade="save-update">
session.save(custmoer);
tr.commit();
session.close();
}
上一篇: javascript如何格式化时间戳函数(附代码)
下一篇: python实现希尔排序的实例详解
推荐阅读
-
hibernate4注解配置,纠结的一对多双向关联
-
hibernate4注解配置,纠结的一对多双向关联
-
Hibernate框架JPA环境一对多配置,级联操作
-
Hibernate框架双向一对多配置,以及级联操作
-
Hibernate框架双向多对多配置,以及级联操作
-
Hibernate框架--学习笔记(中):一对多配置、多对多配置
-
hibernate框架多对多的配置文件以及crud操作
-
hibernate中多对多关系的配置以及增删改操作
-
Spring Boot学习笔记(五)Spring Data Jpa 快速上手(四)多对一@ManyToOne、一对多@OneToMany,以及级联操作
-
Hibernate框架jpa双向多对多配置,操作