Spring Data Paging and Sorting Repository不删除数据

时间:2016-02-22 20:08:11

标签: spring spring-mvc spring-data

所以我试图做的就是允许用户在我的应用程序中删除他们自己的评论和帖子,我有一个表格应该运行一个应该删除它的控制器方法,但它& #39;不工作。

我会告诉你我的控制器和存储库,向你们展示我想要做的事情。

所以这是我的控制器方法

  @RequestMapping(value="userEdits/editComment/{commentId}/deleteComment", method=RequestMethod.POST)
  public String deleteComment (@PathVariable Long commentId, @AuthenticationPrincipal User user)
  {
    Comment comment = commentRepo.findOne(commentId);

    User savedUser = userRepo.findUserByUsername(user.getUsername());

    savedUser.getCourses().remove(comment);

    commentRepo.delete(comment);

    return "redirect:/userEdits";
  }

我甚至可以在调试模式下运行它,并看到正确的注释位于commentRepo.delete(comment);行。并且它会一直运行并返回userEdits屏幕,就像它应该的那样,没有任何错误,但是在它运行完所有内容之后评论仍然存在。

这是我的存储库类,它非常简单,但谁知道,我可能会遗漏一些东西。

public interface CommentRepository extends PagingAndSortingRepository <Comment, Long>{

public Page<Comment> findByPostOrderByIdDesc(Post post, Pageable pageable);

public List<Comment> findByUserOrderByIdDesc(User user);

}

我感到困惑,因为这应该是一项简单的任务,看起来它正在运行并返回我告诉它的视图,没有错误。

因此,如果有人能够看到我出错的地方,那就太好了。提前谢谢。

更新

用户实体

@Entity
@Table(name = "users")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class User {
private Long id;
@ValidEmail
@NotNull
@NotEmpty
private String email;
private String username;
private String password;
private University university;
private Set<Authorities> authorities = new HashSet<>();
private Set<Course> courses = new HashSet<>();
private Set<Post> posts = new HashSet<>();
private Set<Comment> comments = new HashSet<>();
private Set<StudySet> studySet = new HashSet<>();

private Set<Course> myCourses = new HashSet<Course>();

public User() {

}

public User(User user) {
    this.id = user.getId();
    this.email = user.getEmail();
    this.username = user.getUsername();
    this.password = user.getPassword();
    this.university = user.getUniversity();
    this.authorities = user.getAuthorities();
    this.courses = user.getCourses();
    this.posts = user.getPosts();
    this.comments = user.getComments();
    this.studySet = user.getStudySet();
    this.myCourses = user.getMyCourses();
}

@Id
@GeneratedValue
public Long getId() {
    return id;
}

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

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user", orphanRemoval = true)
public Set<Course> getCourses() {
    return courses;
}

public void setCourses(Set<Course> courses) {
    this.courses = courses;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user", orphanRemoval = true)
public Set<Post> getPosts() {
    return posts;
}

public void setPosts(Set<Post> posts) {
    this.posts = posts;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

@ManyToOne
public University getUniversity() {
    return university;
}

public void setUniversity(University university) {
    this.university = university;
}

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "user")
@JsonManagedReference
@JsonIgnoreProperties(allowGetters = true, value = "user")
public Set<Comment> getComments() {
    return comments;
}

public void setComments(Set<Comment> comments) {
    this.comments = comments;
}

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "user")
public Set<Authorities> getAuthorities() {
    return authorities;
}

public void setAuthorities(Set<Authorities> authorities) {
    this.authorities = authorities;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user", orphanRemoval = true)
public Set<StudySet> getStudySet() {
    return studySet;
}

public void setStudySet(Set<StudySet> studySet) {
    this.studySet = studySet;
}

@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinTable(name = "user_myCourses")
public Set<Course> getMyCourses() {
    return myCourses;
}

public void setMyCourses(Set<Course> myCourses) {
    this.myCourses = myCourses;
}
}

评论实体

@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class Comment {

public Long id;
@Size(min = 1, max = 140)
public String comment;
public Post post;
public User user;
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "dd-MMM-YYYY")
private LocalDate date;
@Temporal(TemporalType.TIME)
@DateTimeFormat(pattern = "HH:mm:ss")
private LocalTime time;
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "MM/dd/yyyy HH:mm:ss")
private LocalDateTime dateTime;

public Comment() {

}

@Id
@GeneratedValue
public Long getId() {
    return id;
}

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

@Size(min = 1, max = 140)
public String getComment() {
    return comment;
}

public void setComment(String comment) {
    this.comment = comment;
}

@ManyToOne
public Post getPost() {
    return post;
}

public void setPost(Post post) {
    this.post = post;
}

@ManyToOne
@JsonBackReference
@JsonIgnoreProperties(value = { "comments" }, allowGetters = true)
public User getUser() {
    return user;
}

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

public LocalDate getDate() {
    return date;
}

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

public LocalTime getTime() {
    return time;
}

public void setTime(LocalTime time) {
    this.time = time;
}

public LocalDateTime getDateTime() {
    return dateTime;
}

public void setDateTime(LocalDateTime dateTime) {
    this.dateTime = dateTime;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Comment other = (Comment) obj;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    return true;
}

public Comment(Long id, String comment, Post post, User user, LocalDate date, LocalDateTime dateTime) {
    this.id = id;
    this.comment = comment;
    this.post = post;
    this.user = user;
    this.date = date;
    this.dateTime = dateTime;
}

}

更新

所以我意识到我需要将orphanRemoval = true添加到用户的注释中,现在我在运行控制器方法时收到错误the entity must not be null,但它会删除注释。但我需要我的应用程序来运行该方法并返回我要求的视图,而不会弹出错误消息。

1 个答案:

答案 0 :(得分:1)

问题在于您的CascadeType。你已经指定:

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user", orphanRemoval = true)

CascadeType.ALL表示评论由用户实体管理,您无法直接删除评论。您应该阅读级联类型并根据需要进行更改,或者您有orphanRemoval = true所以您可以在删除后保存用户,并删除评论,例如

savedUser.getCourses().remove(comment);
userRepo.save(savedUser);