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

时间:2018-04-27 08:41:11

标签: hibernate spring-boot jpa spring-data-jpa

所以我有一个schedule对象,其中包含DateActiveScheduleItem的列表。我从数据库中获取调度,从列表中删除DateActiveScheuleItem,并使用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

Schedule.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;
}
 }

DateActiveScheduleItem

@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;
}
 }

在我的控制器中

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

3 个答案:

答案 0 :(得分:0)

最常见的问题是覆盖您的收藏而不是添加收藏。

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

post.dateActiveScheduleItems = list;

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

post.dateActiveScheduleItems.addAll(list);

你应该使用你的集合的一个实例,只需清除它,添加它,永远不会覆盖。

答案 1 :(得分:0)

从此处删除 orphanRemoval = true

@OneToMany(mappedBy = "schedule",cascade = CascadeType.ALL,orphanRemoval=true)

答案 2 :(得分:0)

这也可能发生在(spring boot 应用程序)测试上下文中,maven-surefire-plugin repeatForks 设置为 true。在这种情况下,原因是在测试之间泄漏的数据库记录。

要解决这个问题,要么在运行测试类后正确清理测试环境,要么在运行测试类之前清理数据库测试环境。