分离的实体已通过以保留:JPA OneToMany关系

时间:2018-08-22 03:20:03

标签: java hibernate spring-boot jpa

我试图与孩子一起建立保存实体,但是我找不到它的工作方式,我尝试在所有教程中都这样做,但是始终有一个错误的分离实体传递给持久对象 这是我的实体:

[list(map(int, x.split(','))) for x in s.split(';')]
#[[1, 2000, 5, 1], [1, 2050, 5, 2], [2, 3000, 10, 3]]

和子实体:

@Entity
@Table(name = "CurriculumVitaes")
public class CurriculumVitae {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotBlank
    private String title;

    private String summary;

    @OneToMany(mappedBy = "curriculumVitae", targetEntity = Education.class, cascade={CascadeType.ALL})
    private List<Education> educations = new ArrayList<>();

    public CurriculumVitae(){}

    public CurriculumVitae(Long id, String title, String summary){
        this.id = id;
        this.title = title;
        this.summary = summary;
    }

    public void addEducation(Education education) {
        if(this.educations.contains(education)){
            return;
        }
        this.educations.add(education);
        education.setCurriculumVitae(this);
    }

    public void removeEducation(Education education){
        if(this.educations.contains(education)){
            return;
        }
        this.educations.remove(education);
        education.setCurriculumVitae(null);
    }

    public boolean equals(Object object) {
        if (object == this)
            return true;
        if ((object == null) || !(object instanceof CurriculumVitae))
            return false;

        final CurriculumVitae a = (CurriculumVitae)object;

        if (id != null && a.getId() != null) {
            return id.equals(a.getId());
        }
        return false;
    }
}

如何将所有保存实体与一个存储库方法保存一起设置? 没有级联,将不会保存内部实体。

@Entity
@Table(name = "Educations")
public class Education {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotBlank
    private String title;

    private String areaOfStudy;

    private String description;   

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "curriculumVitaeId", referencedColumnName = "id")
    private CurriculumVitae curriculumVitae;

    public Education(){}

    public Education(String title, String areaOfStudy, String description){
        this.title =  title;
        this.areaOfStudy = areaOfStudy;
        this.description = description;
    }

    public CurriculumVitae getCurriculumVitae() {
        return curriculumVitae;
    }

    public void setCurriculumVitae(CurriculumVitae curriculumVitae) {
        //prevent endless loop
        if (sameAsFormer(curriculumVitae))
            return ;
        //set new owner
        CurriculumVitae oldOwner = this.curriculumVitae;
        this.curriculumVitae = curriculumVitae;
        //remove from the old owner
        if (oldOwner!=null)
            oldOwner.removeEducation(this);
        //set myself into new owner
        if (curriculumVitae!=null)
            curriculumVitae.addEducation(this);
    }
    private boolean sameAsFormer(CurriculumVitae newOwner) {
        return this.curriculumVitae==null? newOwner == null : this.curriculumVitae.equals(newOwner);
    }

    public boolean equals(Object object) {
        if (object == this)
            return true;
        if ((object == null) || !(object instanceof Education))
            return false;

        final Education a = (Education)object;

        if (id != null && a.getId() != null) {
            return id.equals(a.getId());
        }
        return false;
    }
}

0 个答案:

没有答案