如何修复"传递给分离的实体持续存在"

时间:2017-10-11 19:57:31

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

我有3个实体:工作,日程和小组,它们之间的关系是: 时间表有多个组和多个工作; 工作有多个组,可能会或可能不会从与

相关的计划中获得

当我创建一个计划时,我指定了它的组,然后我创建了一个作业,其日程安排是刚创建的,作业的组是该日程表中的组,然后我收到此错误消息"分离的实体传递给persist"。我尝试将级联类型更新为所有类型的组合,但仍然无法工作。请帮忙。

这是我的代码:

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "groups")
public class Group {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "group_guid", nullable = false, unique = true)
    private String groupId;
}

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "schedules")
public class Schedule {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    ....
    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
    @JoinTable(name = "schedules_to_groups",
            joinColumns = @JoinColumn(name = "schedule_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name="group_id", referencedColumnName = "id"))
    private List<Group> groups;

     @OneToMany(mappedBy = "schedule", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<Job> job;

...

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "jobs")
public class Job {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "jobs_to_groups",
            joinColumns = @JoinColumn(name = "job_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name="group_id", referencedColumnName = "id"))
    private List<Group> groups;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "schedule_fk")
    private Schedule schedule;

创建工作的代码:

List<Schedule> scheduleList = scheduleRepository.findAll();
 for (Schedule schedule : scheduleList) {
            Job job = new Job();
            job.setSchedule(schedule);
            job.setGroups(schedule.getGroups());  <--- get it from schedule
            jobRepository.save(job);
...
}

我收到错误消息:

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.xxx.Group

我试图删除&#39; cascade = CascadeType.ALL&#39;从Job类,上面的代码传递,但是下面的代码失败了:

    Group group6 = new Group();
    group6.setGroupId("group6");

    Job job = new Job();
    job.setGroups(Lists.newArrayList(group6));
    jobRepository.save(job);  <--- failed here

错误信息是:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.xxx.ReportGroup

最后我尝试了“cascade = CascadeType.MERGE&#39;在Job on groups字段中,我看到一个甚至奇怪的行为,最初的时间表有3组,在下面的代码之后,时间表和3组之间的关系被删除,而工作与这3组有关系,但我期望的是两个时间表和工作有这3组。

List<Schedule> scheduleList = scheduleRepository.findAll();
     for (Schedule schedule : scheduleList) {
                Job job = new Job();
                job.setSchedule(schedule);
                job.setGroups(schedule.getGroups());  <--- get it from schedule
                jobRepository.save(job);
    ...
    }

当我查看日志时,我发现了这个:

Hibernate: 
    delete 
    from
        `schedules_to_groups` 
    where
        schedule_id=?
Hibernate: 
    insert 
    into
        `
        jobs_to_groups` (
            job_id, group_id
        ) 
    values
        (?, ?)

所以hibernate在插入作业及其组之前删除了日程表和组之间的关系,但为什么呢?

0 个答案:

没有答案