所以我想要做的是允許用戶刪除他們自己的意見和帖子在我的應用程序,我有一個表格,應該運行一個控制器方法,應該刪除它,但它不起作用。春季數據分頁和排序倉庫不刪除數據
我會告訴你我的控制器和存儲庫,告訴你們我想要做什麼。
因此,這裏是我的控制器方法
@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
,但它確實刪除了評論。但我需要我的應用程序來運行該方法並返回我要求的視圖,而不會彈出錯誤消息。
您可以添加您的用戶和評論的實體被刪除。 –
當然,我現在補充說 – dochsner
@Orest我更新了它 – dochsner