Java 为什么';已删除的实体传递给persist'-是否提出了例外?
我正在尝试创建一个简单的web应用程序,它将包含主题和评论。 主题模型:Java 为什么';已删除的实体传递给persist'-是否提出了例外?,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在尝试创建一个简单的web应用程序,它将包含主题和评论。 主题模型: @Entity @Table(name = "T_TOPIC") public class Topic { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @ManyToOne @JoinColumn(name="USER_ID") private User author; @En
@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);
}
}
评论服务:
@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";
}
已删除的实体传递给persist:[com.epam.mvc3.model.Comment#]如果您随后从存储库中删除了主题集合中的Comment对象,则不必从该集合中删除该对象,我认为这样做会导致您的合并删除该注释,删除操作会尝试删除并删除已删除的注释 尝试更改它以从主题中删除注释,然后改为保存主题。或者在服务中创建一个方法,该方法运行查询来计算给定主题的注释数,而不读取整个集合 尽管注释获取类型是渴望的,那么为什么不直接使用
comments.size()
?如下更改您的deleteComent()
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保存/合并主题 你能显示完整的堆栈跟踪吗?另外,请确保您没有与
注释
(例如,来自用户
)的其他@OneToMany
关系。如果没有stacktrace,您的存储库很难合并您传回的对象,然后尝试删除吗?可能是因为您在没有主题的情况下合并了注释,然后删除调用尝试删除已删除的内容而删除了注释吗?
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;
}