【问题标题】:Getting detached entity passed to persist with @ManyToMany bidirectional relationship让分离的实体通过 @ManyToMany 双向关系持续存在
【发布时间】:2016-11-27 15:50:59
【问题描述】:

我很难将实体与 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 - OK

场景 2:使用现有 BEntity 保存 AEntity(id = db 中存在的 id)

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

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

有人可以帮忙吗?谢谢。

【问题讨论】:

    标签: java spring hibernate jpa spring-data


    【解决方案1】:

    你可以试试……

    在第二个实体中:

    @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;
    

    【讨论】:

    • 谢谢您的回复,但是拥有实体应该是AEntity,没有“mappedBy”的那个,所以上面的代码在这种情况下不合适,因为弱实体(一个有mappedBy)无法保存/更新拥有实体(在您的情况下为 BEntity)。
    【解决方案2】:

    终于解决了我的问题。这是 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;
    }
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 2016-10-29
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多