繁体   English   中英

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

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

我很难与JPA保持与多对多关系(双向)的实体。 下面是示例代码:

@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 */
}

以下是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()));
   }
}

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

方案2:使用现有BEntity保存AEntity(id =数据库中存在的id)

在方案2中发生以下异常:一直在stackoverflow中寻找相同的问题,并尝试了不同的组合和建议,但没有锁定。

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

有人可以帮忙吗? 谢谢。

你可以试试...

在第二个实体中:

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

在第一个实体中:

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

终于解决了我的问题。 这是DTO到实体转换器的问题(我不好)。

以前的转换器:

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

重构转换器:返回现有实体的引用。

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

谢谢。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM