2016-02-22 76 views
0

所以我想要做的是允许用户删除他们自己的意见和帖子在我的应用程序,我有一个表格,应该运行一个控制器方法,应该删除它,但它不起作用。春季数据分页和排序仓库不删除数据

我会告诉你我的控制器和存储库,告诉你们我想要做什么。

因此,这里是我的控制器方法

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

} 

我很困惑,因为这应该是一个简单的任务,看来,它的运行通过,并返回查看我告诉它,没有错误。

所以,如果任何人都可以看到我要去哪里,那就太好了。提前致谢。

UPDATE

用户实体

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

} 

UPDATE

于是我意识到我需要添加orphanRemoval = true到用户的评论,现在我运行控制器方法时得到错误the entity must not be null,但它确实删除了评论。但我需要我的应用程序来运行该方法并返回我要求的视图,而不会弹出错误消息。

+0

您可以添加您的用户和评论的实体被删除。 –

+0

当然,我现在补充说 – dochsner

+0

@Orest我更新了它 – dochsner

回答

1

问题出在你的CascadeType上。您指定:

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

CascadeType.ALL意味着注释由用户实体来管理,你不能直接删除评论。你应该阅读有关级联类型,并改变它为您的需求,或者您有orphanRemoval = true所以你可以简单地保存用户后删除和评论应该例如

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

谢谢,我已经完成了所有工作 – dochsner