[英]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.