Мой интеграционный тест завершился неудачно из-за следующей ошибки:
Код: Выделить всё
org.springframework.dao.InvalidDataAccessApiUsageException: detached
entity passed to persist: fr.foo.bar.model.Intervenant
Вот мой тест:
Код: Выделить всё
@BeforeEach
void setUp() {
Intervenant newIntervenant = new Intervenant();
newIntervenant.setIdnum("IDNUM_NEW");
newIntervenant.setIdentite("New Intervenant");
Formation formation = new Formation();
formation.setLibelle("Sample Formation");
formation.setCodeApogee("APOGEE123");
formation.setCodeVet("VET789");
formation.setIntervenants(Set.of(newIntervenant));
formationDto = formationMapper.toDto(formation);
}
@Test
@Transactional
void whenUpdateFormation_thenIntervenantsAreUpdated() throws Exception {
FormationDto createdFormationDto = formationService.create(formationDto);
formationRepository.flush();
IntervenantDto newIntervenantDto = new IntervenantDto(null, "New IDNUM", "New Name");
createdFormationDto.intervenants().add(newIntervenantDto);
Intervenant existingIntervenant = new Intervenant();
existingIntervenant.setIdnum("IDNUM_EXISTING");
existingIntervenant.setIdentite("Existing Intervenant");
existingIntervenant = intervenantRepository.saveAndFlush(existingIntervenant);
existingIntervenant = intervenantRepository.findById(existingIntervenant.getId()).orElseThrow();
createdFormationDto.intervenants().add(intervenantMapper.toDto(existingIntervenant));
FormationDto updatedFormationDto = formationService.update(createdFormationDto.id(), createdFormationDto); // detached entity passed to persist: fr.foo.bar.model.Intervenant
[...]
}
Вот update()< /code> метод:
Код: Выделить всё
@Override
@Transactional
public FormationDto update(Long id, FormationDto formationDto) throws Exception {
Formation formation = formationRepository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException(String.format("La formation %d n'existe pas", id)));
Set updatedIntervenants = new HashSet();
for (IntervenantDto intervenantDto : formationDto.intervenants()) {
if (intervenantDto.id() == null) {
IntervenantDto createdDto = intervenantService.create(intervenantDto);
updatedIntervenants.add(intervenantService.toEntity(createdDto));
} else {
Intervenant existing = intervenantService.getEntity(intervenantDto.id());
updatedIntervenants.add(existing);
}
}
formation.setIntervenants(updatedIntervenants);
Collection existingEtudiants = formationEtudiantService.findAllByFormation(formation); // detached entity passed to persist: fr.foo.bar.model.Intervenant
[...]
}
Код: Выделить всё
@Override
public List findAllByFormation(Formation formation) {
return formationEtudiantRepository.findAllByFormation(formation);
}
Код: Выделить всё
public interface FormationEtudiantRepository extends JpaRepository {
List findAllByFormation(Formation formation);
}
Код: Выделить всё
@Entity(name = "FormationEntity")
@Table(name = "formation")
public class Formation implements Identifiable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = "INT UNSIGNED not null")
private Long id;
@Column(name = "libelle", length = 100)
private String libelle;
@Column(name = "code_apogee", length = 100)
private String codeApogee;
@Column(name = "code_vet", length = 100)
private String codeVet;
@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinTable(name = "formation_intervenant",
joinColumns = @JoinColumn(name = "id_formation"),
inverseJoinColumns = @JoinColumn(name = "id_intervenant"))
private Set intervenants = new LinkedHashSet();
public Set getIntervenants() {
return intervenants;
}
public void setIntervenants(Set intervenants) {
this.intervenants = intervenants;
}
}
Код: Выделить всё
@Entity(name = "IntervenantEntity")
@Table(name = "intervenant")
public class Intervenant implements Identifiable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = "INT UNSIGNED not null")
private Long id;
@Column(name = "idnum", nullable = false, length = 50)
private String idnum;
@Column(name = "identite", length = 100)
private String identite;
@Override
public Long getId() {
return id;
}
@ManyToMany(mappedBy = "intervenants")
private Set formations = new LinkedHashSet();
public void setId(Long id) {
this.id = id;
}
public String getIdnum() {
return idnum;
}
public void setIdnum(String idnum) {
this.idnum = idnum;
}
public String getIdentite() {
return identite;
}
public void setIdentite(String identite) {
this.identite = identite;
}
public Set getFormations() {
return formations;
}
public void setFormations(Set formations) {
this.formations = formations;
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... repository