gpt4 book ai didi

java - Hibernate无法正确操作多个包

转载 作者:太空宇宙 更新时间:2023-11-04 13:31:54 26 4
gpt4 key购买 nike

我有一个模型,它与另外 2 个模型有 2 个 oneToMany 关系。当我从第一个包中删除 1 条记录并保存对象时, hibernate 会从第一个包中删除 1 条记录,这是可以的,但它也会从第二个包中删除与第一个包中的记录具有相同索引的记录。我不知道如何修复它。

这是我的模型

所有者:

@Entity
@Table(name = "table_a")
public class Owner extends Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "id", unique = true, nullable = false)
private int id;

@OneToMany(mappedBy = "owner", fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL)
@OrderColumn(name = "position")
private List<Dog> dogs;

@OneToMany(mappedBy = "owner", fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL)
@OrderColumn(name = "position")
private List<Cat> cats;

public Long getId() {
this.id;
}

public void setId(Long id) {
this.id = id;
}

public Dog getDogs() {
return this.dogs;
}

public void setDogs(List<Dog> dogs) {
this.dogs = dogs;
}

public void addDog(Dog dog) {
dog.owner = this;
this.dogs.add(dog);
}

public void removeDog(Dog dog) {
this.dogs.remove(dog);
}

public Dog getCats() {
return this.cats;
}

public void setCats(List<Cat> cats) {
this.cats = cats;
}

public void addCat(Cat cat) {
Cat.owner = this;
this.cats.add(cat);
}

public void removeCat(Cat cat) {
this.cats.remove(cat);
}
}

狗:

@Entity
@Table(name = "table_b")
public class Dog extends Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "id", unique = true, nullable = false)
private int id;

@ManyToOne
@JoinColumn(name = "owner_id")
private Owner owner;

@Column(name = "position")
private int position;

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public int getPosition() {
return this.position;
}

public void setPosition(int index) {
this.position = index;
}

public Owner getOwner() {
return this.owner;
}

public void setOwner(Owner owner) {
this.owner = owner;
}
}

猫:

@Entity
@Table(name = "table_c")
public class Cat extends Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "id", unique = true, nullable = false)
private int id;

@ManyToOne
@JoinColumn(name = "owner_id")
private Owner owner;

@Column(name = "position")
private int position;

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public int getPosition() {
return this.position;
}

public void setPosition(int index) {
this.position = index;
}

public Owner getOwner() {
return this.owner;
}

public void setOwner(Owner owner) {
this.owner = owner;
}
}

假设我们的主人有 2 只猫和 2 只狗。

当我这样做时:

Cat cat2Remove = owner.getCats().get(1);
owner.removeCat(cat2Remove);
session.save(owner);

Hibernate 按我的意图从 table_b 中删除了第二只猫,但它也从 table_c 中删除了第二只狗,我想知道如何以正确的方式防止这种情况发生?

最佳答案

发生这种情况是由于 cascade = CascadeType.ALL,因此您必须删除 CascadeType.ALL 才能了解级联的效果,请参阅 this链接。

如果您只想级联保存更新,请使用cascade="save-update"我不知道您的情况是否需要 orphanRemoval = true 但请查看 these答案,看看您的业务是否需要它。

关于java - Hibernate无法正确操作多个包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32096095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com