jpa级联操作详解4-级联更新(CascadeType.MERGE)

本文通过车库和汽车实体类的示例,介绍了Java Persistence API (JPA) 中级联更新的功能,包括CascadeType.PERSIST、CascadeType.MERGE等注解的使用,并提供了单元测试方法的演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在jpa的应用中级联更新相比其他的不是很常用,但是也很有了解的必要

在这一讲的例子中我们依然以车库和汽车做实体类

Garage.java

package com.hibernate.jpa.bean1;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Garage {

	/**
	 * many to one jpa 多对一 hibernate实现
	 */
	private Integer gid;
	private String garagenum;
	private Set<Auto> autos = new HashSet<Auto>();
	
	@Id @GeneratedValue
	public Integer getGid() {
		return gid;
	}
	public void setGid(Integer gid) {
		this.gid = gid;
	}
	@Column(length=20)
	public String getGaragenum() {
		return garagenum;
	}
	public void setGaragenum(String garagenum) {
		this.garagenum = garagenum;
	}
	@OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage")
	public Set<Auto> getAutos() {
		return autos;
	}
        // CascadeType.PERSIST: 级联保存
	public void setAutos(Set<Auto> autos) {
		this.autos = autos;
	}
	public void addGarageAuto(Auto auto) {
		auto.setGarage(this);
		this.autos.add(auto);
	}

}

Auto.java

package com.hibernate.jpa.bean1;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Auto {

	/**
	 * one to many jpa 一对多关联 hibernate实现
	 */
	private Integer autoId;
	private String autotype;
	private String autonum;
	private Garage garage;

	@Id @GeneratedValue
	public Integer getAutoId() {
		return autoId;
	}
	public void setAutoId(Integer autoId) {
		this.autoId = autoId;
	}
	public String getAutotype() {
		return autotype;
	}
	public void setAutotype(String autotype) {
		this.autotype = autotype;
	}
	public String getAutonum() {
		return autonum;
	}
	public void setAutonum(String autonum) {
		this.autonum = autonum;
	}
	@ManyToOne(cascade={CascadeType.REMOVE,CascadeType.REFRESH})
	@JoinColumn(name="garageid")
	public Garage getGarage() {
		return garage;
	}
        // CascadeType.REMOVE:级联删除 ;  CascadeType.REFRESH:级联刷新 
	public void setGarage(Garage garage) {
		this.garage = garage;
	}

}

单元测试方法

	@Test public void update() {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Garage garage = em.find(Garage.class, 1);
		garage.setGaragenum("RoomAA");
		
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

运行观察

Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?
Hibernate: update Garage set garagenum=? where gid=?

 

(二)添加CascadeType.MERGE注解          //// CascadeType.MERGE 级联合并

	@OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},mappedBy="garage")
	public Set<Auto> getAutos() {
		return autos;
	}

将单元测试方法中的

garage.setGaragenum("RoomAA");

改一下

garage.setGaragenum("RoomBB");

运行update()

Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?
Hibernate: update Garage set garagenum=? where gid=?

可见,这与上次发出的sql语句是一样的

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值