使用Spring数据jpa删除记录

时间:2016-08-02 12:59:18

标签: rest spring-boot spring-data

我有一个实体'能力',该实体与另外两个实体有OneToMany个关系:CandidatCompetenceOffreCompetence,以及ManyToOne关系与GroupCompetence

我有一个休息删除服务,将使用Competence实体的ID如下:

@Secured("ROLE_ADMIN")
    @RequestMapping(value="/competences/{id}",method= RequestMethod.DELETE)
    public void deleteCompetence(@PathVariable Long id) {
        competenceMetier.deleteCompetence(id);
    }

然后deleteCompetence函数将从Competence Repository中调用delete函数,该函数扩展JpaRepository<Competence, Long>如下:

public void deleteCompetence(Long id) {
        competenceRepository.delete(id);
    }

问题在于,当我调用其余的删除方法时,我得到200作为http响应,但身体中没有任何内容,对于日志我也无法在任何地方看到DELETE sql查询,以及实体仍然存在于数据库中。

这是我的实体:

能力:

@Entity
public class Competence implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long codeCompetence;
    private String titre;
    private Boolean activated = true;

    @OneToMany(mappedBy="competence",cascade = CascadeType.ALL)
    private Collection<CandidatCompetence> candidatCompetences;

    @OneToMany(mappedBy="competence",cascade = CascadeType.ALL)
    private Collection<OffreCompetence> offreCompetences;

    @ManyToOne
    @JoinColumn(name = "groupCompetence")
    private GroupCompetence groupCompetence;

    public Long getCodeCompetence() {
        return codeCompetence;
    }

    public void setCodeCompetence(Long codeCompetence) {
        this.codeCompetence = codeCompetence;
    }


    public String getTitre() {
        return titre;
    }
    public void setTitre(String titre) {
        this.titre = titre;
    }

    @JsonIgnore
    @XmlTransient
    public Collection<CandidatCompetence> getCandidatCompetences() {
        return candidatCompetences;
    }

    @JsonSetter
    public void setCandidatCompetences(Collection<CandidatCompetence> candidatCompetences) {
        this.candidatCompetences = candidatCompetences;
    }

    @JsonIgnore
    @XmlTransient
    public Collection<OffreCompetence> getOffreCompetences() {
        return offreCompetences;
    }

    public void setOffreCompetences(Collection<OffreCompetence> offreCompetences) {
        this.offreCompetences = offreCompetences;
    }

    public Competence(String titre) {
        super();
        this.titre = titre;
    }

    public Competence() {
        super();
        // TODO Auto-generated constructor stub
    }

    @JsonIgnore
    @XmlTransient
    public GroupCompetence getGroupCompetence() {
        return groupCompetence;
    }

    @JsonSetter
    public void setGroupCompetence(GroupCompetence groupCompetence) {
        this.groupCompetence = groupCompetence;
    }

    public Boolean getActivated() {
        return activated;
    }

    public void setActivated(Boolean activated) {
        this.activated = activated;
    }
}

OffreCompetence:

@Entity
public class OffreCompetence implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long codeOffreCompetence;
    private String niveauRequis;

    @ManyToOne
    @JoinColumn(name = "competence")
    private Competence competence;

    @ManyToOne
    @JoinColumn(name="offre")
    private Offre offre;


    public Long getCodeOffreCompetence() {
        return codeOffreCompetence;
    }
    public void setCodeOffreCompetence(Long codeOffreCompetence) {
        this.codeOffreCompetence = codeOffreCompetence;
    }
    public String getNiveauRequis() {
        return niveauRequis;
    }
    public void setNiveauRequis(String niveauRequis) {
        this.niveauRequis = niveauRequis;
    }

    public Competence getCompetence() {
        return competence;
    }

    public void setCompetence(Competence competence) {
        this.competence = competence;
    }
    @JsonIgnore
    public Offre getOffre() {
        return offre;
    }
    @JsonSetter
    public void setOffre(Offre offre) {
        this.offre = offre;
    }
    public OffreCompetence(String niveauRequis) {
        super();
        this.niveauRequis = niveauRequis;
    }
    public OffreCompetence() {
        super();
        // TODO Auto-generated constructor stub
    }


}

CandidatCompetence:

@Entity
public class CandidatCompetence implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long codeCandidatCompetence;
    private String niveauExperience;
    @ManyToOne
    @JoinColumn(name = "candidat")
    private Candidat candidat;
    @ManyToOne
    @JoinColumn(name = "competence")
    private  Competence competence;

    public Long getCodeCandidatCompetence() {
        return codeCandidatCompetence;
    }
    public void setCodeCandidatCompetence(Long codeCandidatCompetence) {
        this.codeCandidatCompetence = codeCandidatCompetence;
    }
    public String getNiveauExperience() {
        return niveauExperience;
    }
    public void setNiveauExperience(String niveauExperience) {
        this.niveauExperience = niveauExperience;
    }
    public Candidat getCandidat() {
        return candidat;
    }
    public void setCandidat(Candidat candidat) {
        this.candidat = candidat;
    }
    public Competence getCompetence() {
        return competence;
    }
    public void setCompetence(Competence competence) {
        this.competence = competence;
    }
    public CandidatCompetence(String niveauExperience) {
        super();
        this.niveauExperience = niveauExperience;
    }
    public CandidatCompetence() {
        super();
        // TODO Auto-generated constructor stub
    }


}

GroupCompetence:

@Entity
public class GroupCompetence implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long codeGroupCompetence;
    private String titre;
    private Boolean activated = true;

    @OneToMany(mappedBy="groupCompetence",cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Collection<Competence> competences;

    public Long getCodeGroupCompetence() {
        return codeGroupCompetence;
    }

    public void setCodeGroupCompetence(Long codeGroupCompetence) {
        this.codeGroupCompetence = codeGroupCompetence;
    }

    public String getTitre() {
        return titre;
    }

    public void setTitre(String titre) {
        this.titre = titre;
    }

    public GroupCompetence(String titre) {
        this.titre = titre;
    }

    public GroupCompetence() {
    }

    public Boolean getActivated() {
        return activated;
    }

    public void setActivated(Boolean activated) {
        this.activated = activated;
    }

    public Collection<Competence> getCompetences() {
        return competences;
    }

    public void setCompetences(Collection<Competence> competences) {
        this.competences = competences;
    }
}

1 个答案:

答案 0 :(得分:0)

您应该使用deleteCompetence为您的服务方法@Transactional添加注释。