I am having difficulty persisting entity with many-to-many relationship (bidirectional) with 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...
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
Scenario 2: Saving AEntity with existing BEntity (id = id existing in db)
The following exception occurs in Scenario 2: Been looking for same questions in stackoverflow and tried different combination and suggestion but with no lock.
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).
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.