gpt4 book ai didi

Hibernate:具有级联=“all-delete-orphan”的集合不再被拥有的实体实例引用

转载 作者:行者123 更新时间:2023-12-02 13:48:34 27 4
gpt4 key购买 nike

所以我有一个包含 DateActiveScheduleItem 列表的计划对象。我从数据库获取计划,从列表中删除 DateActiveScheduleItem,并使用 Hibernate CrudRepository 中的 .save() 保存它。

nested exception is org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: haughton.daniel.ShoutLoud.model.schedule.Schedule.dateActiveScheduleItems; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: haughton.daniel.ShoutLoud.model.schedule.Schedule.dateActiveScheduleItems] with root causeorg.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: haughton.daniel.ShoutLoud.model.schedule.Schedule.dateActiveScheduleItems

时间表.java

@Entity
public class Schedule {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

@ManyToOne
@JoinColumn(name = "usergroup_id")
private UserGroup userGroup;

private String description;


private boolean master;//is this a schedule for all supervalus(true) or one supervalu(false)

@OneToMany(mappedBy = "schedule",cascade = CascadeType.ALL,orphanRemoval=true)
private List<DateActiveScheduleItem> dateActiveScheduleItems = new ArrayList<>();

@OneToMany(mappedBy = "schedule",cascade = CascadeType.PERSIST)
private List<MusicScheduleItem> musicScheduleItems = new ArrayList<>();

@OneToMany(mappedBy = "schedule",cascade = CascadeType.PERSIST)
private List<AdvertisementScheduleItem> advertisementScheduleItems = new ArrayList<>();

@Basic
@Temporal(TemporalType.DATE)
private java.util.Date dateAdded;
public Long getId() {
return id;
}

public Date getDateAdded() {
return dateAdded;
}

public void setDateAdded(Date dateAdded) {
this.dateAdded = dateAdded;
}

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

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public UserGroup getUserGroup() {
return userGroup;
}

public void setUserGroup(UserGroup userGroup) {
this.userGroup = userGroup;
}
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}


public boolean isMaster() {
return master;
}

public void setMaster(boolean master) {
this.master = master;
}

public List<DateActiveScheduleItem> getDateActiveScheduleItems() {
return dateActiveScheduleItems;
}

public void setDateActiveScheduleItems(List<DateActiveScheduleItem> dateActiveScheduleItems) {
this.dateActiveScheduleItems = dateActiveScheduleItems;
}

public List<AdvertisementScheduleItem> getAdvertisementScheduleItems() {
return advertisementScheduleItems;
}

public void setAdvertisementScheduleItems(List<AdvertisementScheduleItem> advertisementScheduleItems) {
this.advertisementScheduleItems = advertisementScheduleItems;
}

public List<MusicScheduleItem> getMusicScheduleItems() {
return musicScheduleItems;
}

public void setMusicScheduleItems(List<MusicScheduleItem> musicScheduleItems) {
this.musicScheduleItems = musicScheduleItems;
}
public boolean removeDateActiveScheduleItem(Date date){
for(int i =0;i<dateActiveScheduleItems.size();i++){

if(date.getTime()==dateActiveScheduleItems.get(i).getDate().getTime()){
dateActiveScheduleItems.remove(i);
return true;
}
}
return false;
}
public boolean isScheduleActiveToday(){
Calendar now = Calendar.getInstance();
for(int i=0;i<dateActiveScheduleItems.size();i++){
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(now.getTime());
cal2.setTime(dateActiveScheduleItems.get(i).getDate());
boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
if(sameDay==true){
return true;
}
}

return false;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Schedule schedule = (Schedule) o;

if (master != schedule.master) return false;
if (id != null ? !id.equals(schedule.id) : schedule.id != null) return false;
if (user != null ? !user.equals(schedule.user) : schedule.user != null) return false;
if (userGroup != null ? !userGroup.equals(schedule.userGroup) : schedule.userGroup != null) return false;
if (description != null ? !description.equals(schedule.description) : schedule.description != null)
return false;
if (dateActiveScheduleItems != null ? !dateActiveScheduleItems.equals(schedule.dateActiveScheduleItems) : schedule.dateActiveScheduleItems != null)
return false;
if (musicScheduleItems != null ? !musicScheduleItems.equals(schedule.musicScheduleItems) : schedule.musicScheduleItems != null)
return false;
if (advertisementScheduleItems != null ? !advertisementScheduleItems.equals(schedule.advertisementScheduleItems) : schedule.advertisementScheduleItems != null)
return false;
return dateAdded != null ? dateAdded.equals(schedule.dateAdded) : schedule.dateAdded == null;
}

@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (user != null ? user.hashCode() : 0);
result = 31 * result + (userGroup != null ? userGroup.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (master ? 1 : 0);
result = 31 * result + (dateActiveScheduleItems != null ? dateActiveScheduleItems.hashCode() : 0);
result = 31 * result + (musicScheduleItems != null ? musicScheduleItems.hashCode() : 0);
result = 31 * result + (advertisementScheduleItems != null ? advertisementScheduleItems.hashCode() : 0);
result = 31 * result + (dateAdded != null ? dateAdded.hashCode() : 0);
return result;
}
}

日期事件计划项目

@Entity
public class DateActiveScheduleItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Basic
@Temporal(TemporalType.DATE)
private Date date;

public Schedule getSchedule() {
return schedule;
}

public void setSchedule(Schedule schedule) {
this.schedule = schedule;
}

@JsonIgnore
@ManyToOne
@JoinColumn(name = "schedule_id")
private Schedule schedule;

public Long getId() {
return id;
}

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

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

DateActiveScheduleItem that = (DateActiveScheduleItem) o;

if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (date != null ? !date.equals(that.date) : that.date != null) return false;
return schedule != null ? schedule.equals(that.schedule) : that.schedule == null;
}

@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (date != null ? date.hashCode() : 0);
result = 31 * result + (schedule != null ? schedule.hashCode() : 0);
return result;
}
}

在我的 Controller 中

保存 DateActiveScheduleItem 列表中包含任何内容的任何时间表时,会发生此错误

最佳答案

最常见的问题是覆盖您的集合而不是添加到其中。

如果你碰巧做了类似的事情:

post.dateActiveScheduleItems = list;

这可能会导致以下问题。而不是尝试这个。

post.dateActiveScheduleItems.addAll(list);

您应该使用集合的一个实例,只需清除它,添加到其中,从不覆盖。

关于Hibernate:具有级联=“all-delete-orphan”的集合不再被拥有的实体实例引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50058553/

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