Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从可选Java对象中的数组中删除所有项?_Java_Spring Boot_Mapstruct - Fatal编程技术网

如何从可选Java对象中的数组中删除所有项?

如何从可选Java对象中的数组中删除所有项?,java,spring-boot,mapstruct,Java,Spring Boot,Mapstruct,我在Post和标记(主题)实体之间有很多关系。因此,在我可以删除一篇文章之前,我需要获取它,删除它的标签和主题,保存文章,然后删除它,但是我找不到通过Post对象并删除标签和主题项目(如果有)的方法 我得到一个:java.util.ConcurrentModificationException:null @Override public void delete(Long id) { log.debug("Request to delete Post : {}", id); Opt

我在Post和标记(主题)实体之间有很多关系。因此,在我可以删除一篇文章之前,我需要获取它,删除它的标签和主题,保存文章,然后删除它,但是我找不到通过Post对象并删除标签和主题项目(如果有)的方法

我得到一个: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);
}
删除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;
}
publicsetgettags(){
返回标签;
}
公共邮政标签(设置标签){
this.tags=标签;
归还这个;
}
公共Post添加标签(标签标签){
this.tags.add(tag);
tag.getPosts().add(这个);
归还这个;
}
公共邮政移除标签(标签){
此.tags.remove(标记);
tag.getPosts().remove(这个);
归还这个;
}
公共无效集合标记(集合标记){
this.tags=标签;
}
公共集getTopics(){
返回主题;
}
公共帖子主题(设置主题){
this.topics=主题;
归还这个;
}
公共帖子添加主题(主题){
this.topics.add(topic);
topic.getPosts().add(这个);
归还这个;
}
公共Post removeTopic(主题){
this.topics.remove(主题);
topic.getPosts().remove(这个);
归还这个;
}
删除标记中的方法:

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;
}
publicsetgetposts(){
返回岗位;
}
公共标杆(固定标杆){
这个.posts=posts;
归还这个;
}
公共标签addPost(Post-Post){
this.posts.add(post);
post.getTags().add(this);
归还这个;
}
公共标记移除Post(Post Post){
此.posts.移除(post);
post.getTags().remove(这个);
归还这个;
}
公共无效设置柱(设置柱){
这个.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);
}   
@覆盖
公共无效删除(长id){
debug(“请求删除Post:{}”,id);
可选postOpt=postRepository.findById(id);
Post Post=postOpt.get();
ArrayList arrayTags=新的ArrayList();
addAll(post.getTags());
迭代器copyOfTags=arrayTags.Iterator();
while(copyOfTags.hasNext()){
Tag Tag=copyOfTags.next();
标记。移除柱(柱);
}
ArrayList arrayTopics=新的ArrayList();
addAll(post.getTopics());
迭代器copyOfTopics=arrayTopics.Iterator();
while(copyOfTopics.hasNext()){
Topic Topic=copyOfTopics.next();
topic.removePost(post);
}
postRepository.save(post);
postRepository.deleteById(id);
postSearchRepository.deleteById(id);
}   

再次感谢,给您带来不便,非常抱歉

不能迭代集合,同时从集合中删除元素

Set<Tag> copyOfTags = new HashSet<Tag>(post.getTags());
 for (Tag tag : copyOfTags) {
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + tag.toString());
        post.removeTag(tag);
    }
您希望通过方法removeTagremoveTopic删除标记和主题,因此不希望使用迭代删除方法

最好是创建标记集合的副本,并在复制的集合上迭代

Set<Tag> copyOfTags = new HashSet<Tag>(post.getTags());
 for (Tag tag : copyOfTags) {
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!" + tag.toString());
        post.removeTag(tag);
    }
Set copyOfTags=newhashset(post.getTags());
用于(标签标签:copyOfTags){
System.out.println(“!!!!!!!!!!!!!!!!!!!!!”+tag.toString());
拆后标签(tag);
}

出现异常的行号是多少?您的DTO是否真的很重要?看起来您的实体上的配置不好,因为您可能只是删除Post实体,并且必须删除依赖的实体。你能发布你的模型吗?请发布你的“删除”方法或完整实体类。说明你的答案与我使用
数组给出的答案不同。asList
。在您的情况下,您假设它的集合,而我假设它是
LIST
?检查asList()方法的api,asList方法不会创建集合的副本。它将使用一个将被设置的元素创建列表。@SafeVarargs@SuppressWarnings(“varargs”)public static list asList(T…a){return new ArrayList(a);}这不会创建集合的副本。它将返回列表,但不会返回列表。所以你不能像(标记t:toListReturn)那样迭代它。你明白我的意思吗?Set只是一个例子。getTags方法返回集合,因为他能够迭代该返回。因此toList将返回列表,其中包含一个元素,该元素将是toList方法返回的集合。您期望toList方法将返回包含getTags返回的集合中的元素的列表,这是错误的假设。