简体   繁体   English

通过获取分离实体以保持@ManyToMany双向关系

[英]Getting detached entity passed to persist with @ManyToMany bidirectional relationship

I am having difficulty persisting entity with many-to-many relationship (bidirectional) with JPA. 我很难与JPA保持与多对多关系(双向)的实体。 Below are the sample code: 下面是示例代码:

@Entity
@Table(name = "aentity")
public class AEntity {
    @Id
    @Column(name = "id",
            unique = true)
    @TableGenerator(initialValue = 1,
            name = "aentity_id_generator",
            pkColumnName = "table_name",
            pkColumnValue = "aentity",
            table = "id_generator",
            valueColumnName = "id")
    @GeneratedValue(generator = "aentity_id_generator",
            strategy = GenerationType.TABLE)
    private BigInteger id;

    @JoinTable(name = "bentity_aentities")
    @ManyToMany
    private Set<BEntity> bentities;

    /* getters and setters follows */
}

@Entity
@Table(name = "bentity")
public class BEntity {
    @Id
    @Column(name = "id",
            unique = true)
    @TableGenerator(initialValue = 1,
            name = "bentity_id_generator",
            pkColumnName = "table_name",
            pkColumnValue = "bentity",
            table = "id_generator",
            valueColumnName = "id")
    @GeneratedValue(generator = "bentity_id_generator",
            strategy = GenerationType.TABLE)
    private BigInteger id;

    @ManyToMany(mappedBy = "bentities")
    private Set<AEntity> aentities;

    /* getters and setters follows */
}

Below is the dto to entity converter... 以下是dto到实体的转换器...

public class DtoToEntityConverter {
   public void convertToEntity(AEntityDto aDto, AEntity a) {
      a.setBEntities(aDto.getBEntities().parallelStream().
         .map(bDto -> {
            return toBEntity(bDto); //this will just copy/transfer the properties from bEntityDto to bEntity.
         })
         .collect(Collectors.toSet()));
   }
}

Scenario 1: Saving AEntity with BEntity (id = null) - OK 方案1:使用BEntity(id = null)保存AEntity-确定

Scenario 2: Saving AEntity with existing BEntity (id = id existing in db) 方案2:使用现有BEntity保存AEntity(id =数据库中存在的id)

The following exception occurs in Scenario 2: Been looking for same questions in stackoverflow and tried different combination and suggestion but with no lock. 在方案2中发生以下异常:一直在stackoverflow中寻找相同的问题,并尝试了不同的组合和建议,但没有锁定。

detached entity passed to persist: BEntity; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: BEntity" 

Can anybody help please. 有人可以帮忙吗? Thanks. 谢谢。

You can try... 你可以试试...

In the second entity: 在第二个实体中:

@ManyToMany(cascade=CascadeType.ALL, mappedBy="bentities")
private Set<AEntity> aentities;

In the first entity: 在第一个实体中:

@ManyToMany(cascade=CascadeType.ALL) 
@JoinTable(name="bentity_aentities", joinColumns=@JoinColumn(name="aentity_id"), inverseJoinColumns=@JoinColumn(name="bentity_id")) 
private Set<BEntity> bentities;

Finally solved my problem. 终于解决了我的问题。 It was the issue in DTO to Entity converter (my bad). 这是DTO到实体转换器的问题(我不好)。

Previous converter: 以前的转换器:

public AEntity toAEntity(@NotNull AEntityDto aentityDto) {
     AEntity aentity = new AEntity();
     copyProperties(aentityDto, aentity);
     return aentity;
}

Refactored converter: Return the reference of the existing entity. 重构转换器:返回现有实体的引用。

public AEntity toAEntity(@NotNull AEntityDto aentityDto) {
     AEntity aentity = aRepository.findSingleByTitle(aentityDto.getTitle());
     if(aentity == null) {
         aentity = new AEntity();
         copyProperties(aentityDto, aentity);
     }
     return aentity;
}

Thanks. 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 数据 JPA 和 hibernate 分离的实体传递以保持多对多关系 - Spring data JPA and hibernate detached entity passed to persist on ManyToMany relationship Hibernate:将现有的子实体添加到具有 OneToMany 双向关系的新实体并持久化(“分离的实体传递给持久化”) - Hibernate: add existing child entity to new entity with OneToMany bidirectional relationship and persist it ('detached entity passed to persist') “分离的实体传递给持久化”当我尝试持久化具有多对多关系的实体时 - “detached entity passed to persist” When i try to persist an Entity with ManyToMany relationship 分离的实体传递给持久对象:一对一关系 - detached entity passed to persist: one to one to relationship org.hibernate.PersistentObjectException:分离的实体传递给持久化-ManyToMany映射 - org.hibernate.PersistentObjectException: detached entity passed to persist - ManyToMany Mapping 带有CascadeType.ALL的ManyToMany导致传递的Detched实体持久化 - ManyToMany with CascadeType.ALL results in Detached entity passed to persist 传递给持久化的分离实体 - detached entity passed to persist 分离的实体已传递以保留 - Detached entity passed to persist 当我第一次检索必须在 @ManyToMany 关系中使用的对象时,为什么会获得这个“传递给持久化的分离实体”? - Why am I obtaining this "detached entity passed to persist" when I first retrieve the object that has to be used in a @ManyToMany relationship? 独立实体通过以一对多关系持久化Spring - Detached entity passed to persist Spring in one to many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM