У меня много отношений между Post и Tag (Тема). Поэтому, прежде чем я смогу удалить сообщение, мне нужно его получить, удалить его теги и темы, сохранить сообщение, а затем удалить его, но я не могу найти способ просмотреть объект сообщения и удалить элементы тегов и тем, если они есть. любой.
Я получаю: java.util.ConcurrentModificationException: null
@Override
public void delete(Long id) {
log.debug("Request to delete Post : {}", id);
Optional<Post> postOpt = postRepository.findById(id);
Post post = postOpt.get();
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + post);
for (Tag tag : post.getTags()) {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + tag.toString());
post.removeTag(tag);
}
for (Topic topic : post.getTopics()) {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + topic.toString());
post.removeTopic(topic);
}
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + post);
// postRepository.deleteById(id);
// postSearchRepository.deleteById(id);
}
Где объект DTO такой:
[
{
"id": 0,
"publicationDate": "2019-01-07T10:24:15.892Z",
"quote": "string",
"tags": [
{
"id": 0,
"tagName": "string"
}
],
"topics": [
{
"id": 0,
"topicName": "string"
}
],
"userId": 0,
}
]
Методы удаления в POST:
public Set<Tag> getTags() {
return tags;
}
public Post tags(Set<Tag> tags) {
this.tags = tags;
return this;
}
public Post addTag(Tag tag) {
this.tags.add(tag);
tag.getPosts().add(this);
return this;
}
public Post removeTag(Tag tag) {
this.tags.remove(tag);
tag.getPosts().remove(this);
return this;
}
public void setTags(Set<Tag> tags) {
this.tags = tags;
}
public Set<Topic> getTopics() {
return topics;
}
public Post topics(Set<Topic> topics) {
this.topics = topics;
return this;
}
public Post addTopic(Topic topic) {
this.topics.add(topic);
topic.getPosts().add(this);
return this;
}
public Post removeTopic(Topic topic) {
this.topics.remove(topic);
topic.getPosts().remove(this);
return this;
}
Методы удаления в тегах:
public Set<Post> getPosts() {
return posts;
}
public Tag posts(Set<Post> posts) {
this.posts = posts;
return this;
}
public Tag addPost(Post post) {
this.posts.add(post);
post.getTags().add(this);
return this;
}
public Tag removePost(Post post) {
this.posts.remove(post);
post.getTags().remove(this);
return this;
}
public void setPosts(Set<Post> posts) {
this.posts = posts;
}
Спасибо
ПОСЛЕ ИЗМЕНЕНИЙ: Работает.
@Override
public void delete(Long id) {
log.debug("Request to delete Post : {}", id);
Optional<Post> postOpt = postRepository.findById(id);
Post post = postOpt.get();
ArrayList<Tag> arrayTags = new ArrayList<Tag>();
arrayTags.addAll(post.getTags());
Iterator<Tag> copyOfTags = arrayTags.iterator();
while (copyOfTags.hasNext()) {
Tag tag = copyOfTags.next();
tag.removePost(post);
}
ArrayList<Topic> arrayTopics = new ArrayList<Topic>();
arrayTopics.addAll(post.getTopics());
Iterator<Topic> copyOfTopics = arrayTopics.iterator();
while (copyOfTopics.hasNext()) {
Topic topic = copyOfTopics.next();
topic.removePost(post);
}
postRepository.save(post);
postRepository.deleteById(id);
postSearchRepository.deleteById(id);
}
Еще раз спасибо и извините за доставленные неудобства!