Hibernate框架JPA环境一对多配置,级联操作
程序员文章站
2022-04-24 14:31:19
...
一的持久化类
package bean;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
//类上:
// @Entity // 当前类是持久化类
// @Table(name="表名") //当前持久化类和哪个表做映射
//
// 属性:
// @Id //当前的属性是oid属性
// @Column(name="主键名")// 和表的哪个字段做映射
// @GeneratedValue(strategy=GenerationType.IDENTITY) //指定oid的增长策略
//
//
// @Column(name="字段名") // 其它属性和表中的字段映射
@Entity
@Table(name = "cst_customer")
public class Customer {
@Id
@Column(name = "cust_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long cust_id;// '客户编号(主键)',
@Column(name = "cust_name")
private String cust_name;// '客户名称(公司名称)',
@Column(name = "cust_source")
private String cust_source;// '客户信息来源',
@Column(name = "cust_industry")
private String cust_industry;// '客户所属行业',
@Column(name = "cust_level")
private String cust_level;// '客户级别',
@Column(name = "cust_address")
private String cust_address;// '客户联系地址',
@Column(name = "cust_phone")
private String cust_phone;// '客户联系电话',
// 配置1对多的关系
// targetEntity: 对方的字节码文件类型
// mappedBy:自己在对方中的属性名 出现哪一方 意味着哪一方不会去维护外键了
/*cascade
CascadeType.All 即使级联保存 又是级联删除 save-update,delete
CascadeType.PERSIST 级联保存 save-update
CascadeType.REMOVE 级联删除 delete
*/
@OneToMany(targetEntity=Linkman.class,mappedBy="custmoer",cascade=CascadeType.ALL)
private Set<Linkman> linkmans=new HashSet<Linkman>();
public Set<Linkman> getLinkmans() {
return linkmans;
}
public void setLinkmans(Set<Linkman> linkmans) {
this.linkmans = linkmans;
}
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_address() {
return cust_address;
}
public void setCust_address(String cust_address) {
this.cust_address = cust_address;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
@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_address=" + cust_address
+ ", cust_phone=" + cust_phone + "]";
}
}
多的持久化类
package bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "cst_linkman")
public class Linkman {
@Id
@Column(name = "lkm_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long lkm_id;// '联系人编号(主键)',
@Column(name = "lkm_name")
private String lkm_name;// '联系人姓名',
@Column(name = "lkm_gender")
private String lkm_gender;// '联系人性别',
@Column(name = "lkm_phone")
private String lkm_phone;// '联系人办公电话',
@Column(name = "lkm_mobile")
private String lkm_mobile;// '联系人手机',
@Column(name = "lkm_email")
private String lkm_email;// '联系人邮箱',
@Column(name = "lkm_position")
private String lkm_position;// '联系人职位',
@Column(name = "lkm_memo")
private String lkm_memo;// '联系人备注',
// 外键
// 配置1对多的关系
// 有一个客户的对象
// targetEntity:对方的字节码文件对象类型
@ManyToOne(targetEntity = Customer.class)
// 维护外键关系
/*
* name:外键字段名 referencedColumnName:指向的主键字段名
*/
@JoinColumn(name = "wj_id", referencedColumnName = "cust_id")
private Customer 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 Customer getCustmoer() {
return custmoer;
}
public void setCustmoer(Customer custmoer) {
this.custmoer = custmoer;
}
}
级联操作
package test;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.junit.Test;
import bean.Customer;
import bean.Linkman;
import utils.JPAUtils;
public class demo3 {
//普通保存
@Test
public void t1() {
EntityManager en = JPAUtils.getEntityManager();
EntityTransaction tr = en.getTransaction();
tr.begin();
Customer customer=new Customer();
customer.setCust_name("老马");
Linkman linkman1=new Linkman();
linkman1.setLkm_name("大密");
Linkman linkman2=new Linkman();
linkman2.setLkm_name("大密");
Linkman linkman3=new Linkman();
linkman3.setLkm_name("大密");
//关联客户和联系人
customer.getLinkmans().add(linkman1);
customer.getLinkmans().add(linkman2);
customer.getLinkmans().add(linkman3);
linkman1.setCustmoer(customer);
linkman2.setCustmoer(customer);
linkman3.setCustmoer(customer);
//保存
en.persist(customer);
en.persist(linkman1);
en.persist(linkman2);
en.persist(linkman3);
tr.commit();
en.close();
}
//级联保存
//级联:在操作自己数据的时候 还会把自己关联的数据也操作了
// 级联保存 保存客户同时把客户下面的联系人都保存了
// @OneToMany(cascade=CascadeType.PERSIST)
@Test
public void t2() {
EntityManager en = JPAUtils.getEntityManager();
EntityTransaction tr = en.getTransaction();
tr.begin();
Customer customer=new Customer();
customer.setCust_name("老马1");
Linkman linkman1=new Linkman();
linkman1.setLkm_name("大密1");
Linkman linkman2=new Linkman();
linkman2.setLkm_name("大密1");
Linkman linkman3=new Linkman();
linkman3.setLkm_name("大密1");
//关联客户和联系人
customer.getLinkmans().add(linkman1);
customer.getLinkmans().add(linkman2);
customer.getLinkmans().add(linkman3);
linkman1.setCustmoer(customer);
linkman2.setCustmoer(customer);
linkman3.setCustmoer(customer);
en.persist(customer);
tr.commit();
en.close();
}
@Test //普通删除--报错
public void test3()
{
EntityManager em = JPAUtils.getEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
// 删除一个客户
Customer customer = em.find(Customer.class, 1L);
em.remove(customer);
tx.commit();
em.close();
}
@Test // 级联删除
// 改为ALL:包含保存和删除
//@OneToMany(cascade=CascadeType.ALL)
public void test()
{
EntityManager em = JPAUtils.getEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
// 删除一个客户级联下面关联的联系人
Customer customer = em.find(Customer.class, 2L);
em.remove(customer);
tx.commit();
em.close();
}
}