使用@OneToMany关系更新实体

问题描述

我将类AEntity定义为与ManyToOne双向的关系BEntity

BEntity类中,我与ManyToOneCEntity的关系。

@Entity
public class AEntity {

    @Id
    private String aId;

    @ManyToOne
    @JoinColumn(name = "b_id",updatable = false)
    private BEntity bEntity;
    
}


@Entity
public class BEntity {

    @Id
    private String bId;

    @OnetoMany
    @JsonIgnore
    @JoinColumn(name = "b_id")
    private Set<AEntity> aEntites;
    
    @ManyToOne
    @JoinColumn(name = "cId",updatable = false)
    private CEntity cEntity;
    
}


@Entity
public class CEntity {

    @Id
    private String cId;
    
    @OnetoMany(mappedBy = "cEntity",cascade = CascadeType.ALL)
    private Set<BEntity> bEntites;
    
}

我想更新BEntity添加新数据aEntites,我尝试使用以下代码,但是出现此错误

找不到ID为xxxx的AEntity;嵌套异常为 javax.persistence.EntityNotFoundException:无法找到具有以下内容的AEntity id xxxx

BEntity bEntity = getBEntity(event.agreementId);
            
Set<AEntity> aEntities = bEntity.getAEntites();
aEntities.add(new AEntity(event.aId,bEntity));
            
bEntity.setAEntites(aEntities);
bRepository.save(bEntity);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)