2012-09-11 61 views
1

我試圖創建簡單的網絡應用程序,其中將包含主題和評論。 主題模型:爲什麼'被刪除的實體傳遞給persist'異常被引發?

@Entity 
@Table(name = "T_TOPIC") 
public class Topic { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private int id; 

    @ManyToOne 
    @JoinColumn(name="USER_ID") 
    private User author; 

    @Enumerated(EnumType.STRING)  
    private Tag topicTag; 

    private String name; 
    private String text; 

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "topic", cascade = CascadeType.ALL) 
    private List<Comment> comments; 

    // We shouldn't read whole collection to get this basic info 
    private int commentsCount; 
    private Date timeLastUpdated; 

    /** 
    * Removes comment from the Topic entity 
    * @param comment 
    */ 
    public void removeComment(Comment comment) { 
      // updating topic 
      timeLastUpdated = Utils.getCurrentTime(); 
      commentsCount--; 
      comments.remove(comment); 

    } 
} 

Comment模型:

@Entity 
@Table(name = "T_COMMENT") 
public class Comment 
{ 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private int id; 

    @ManyToOne 
    @JoinColumn(name="TOPIC_ID") 
    private Topic topic; 

    @ManyToOne 
    @JoinColumn(name="USER_ID") 
    private User author; 

    private String text; 
    private Date creationDate; 
} 

評服務:

@Service 
@Transactional 
public class CommentService { 

    @Autowired 
    private CommentRepository commentRepository; 

    public int saveComment(Comment comment){ 
     return commentRepository.save(comment).getId(); 

    } 

    public Comment findCommentByID(int id){ 
     return commentRepository.findOne(id); 
    } 

    /** 
    * Deletes a comment 
    * @param comment -- a comment to delete 
    * @return id of a topic 
    */ 
    public int deleteComment(Comment comment) { 
     int result = comment.getTopic().getId(); 
     commentRepository.delete(comment); 
     return result; 
    } 
} 

控制器的方法,其被刪除的評論:

@RequestMapping(value = "/deleteComment/{commentId}", method = {RequestMethod.POST, RequestMethod.GET}) 
public String deleteComment(@PathVariable int commentId, @ModelAttribute("comment")Comment comment, BindingResult result, Model model){ 
    Topic parentTopic; 
    Comment deletedComment = commentService.findCommentByID(commentId); 
    if (deletedComment != null) { 
     // remove any relations 
     parentTopic = deletedComment.getTopic(); 
     parentTopic.removeComment(deletedComment); 
     // rempve the comment itself 
     commentService.deleteComment(deletedComment); 
     } 
     //return "refresh:"; 
     return "home"; 
} 

刪除的實體傳遞到堅持:[com.epam.mvc3.model.Comment#]

+0

可以顯示完整的堆棧跟蹤嗎?此外,請確保您沒有其他'@ OneToMany'關係到'Comment'(例如,來自'User')。 – axtavt

+0

沒有堆棧跟蹤它很難,但是你的存儲庫合併你傳回來的對象,然後試圖刪除?它可能是刪除評論,因爲你沒有一個主題合併它,然後刪除調用試圖刪除已經消失的東西? – Link19

回答

0

如果您然後從您的存儲庫中刪除它,你不應該從Topic中的集合中刪除Comment對象,米認爲這樣做導致你的合併刪除評論,刪除嘗試刪除和已經刪除評論。

嘗試更改它以刪除主題中的評論,然後保存該主題。或者在運行查詢的服務中創建一個方法來計算給定主題的評論數量,而無需閱讀整個集合。

雖然評論獲取類型很渴望,爲什麼不使用comments.size()

0

更改deleteComment()如下,

public int deleteComment(int commentId) 
{ 

     Comment existingComment = findCommentByID(commentId); 
     if(existingComment != null) 
     { 

      int result = existingComment .getTopic().getId(); 
      Topic existingTopic = existingComment .getTopic(); 
      existingTopic.remove(existingComment) 
      topicRepository.save(existingTopic);// spring 3.1 
      topicRepository.merge(existingTopic);// spring 3.0 
      return result; 

      } 

     return 0; 

    } 

的這裏的一點是,我們需要一直使用父DAO /庫來處理孩子。

因此,首先從主題中刪除評論,並使用topicRepository保存/合併主題。

相關問題