I have this error:
2020-11-16 22:36:09.313 ERROR 19428 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: collection was evicted; nested exception is org.hibernate.HibernateException: collection was evicted] with root cause
org.hibernate.HibernateException: collection was evicted
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:43) ~[hibernate-core-5.4.22.Final.jar:5.4.22.Final]
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102) ~[hibernate-core-5.4.22.Final.jar:5.4.22.Final]
I create an API REST application, my entities have this composition:
public class Label {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long Id;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "label")
private Set<Release> releases;
public class Release {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "label_id")
private Label label;
After the third API-REST request for create a release/label association data i have the error at the top
RestController:
@PostMapping(value = "/release", consumes = "application/json", produces = "application/json")
Release newRelease(@RequestBody ReleaseDto releaseDto) {
return releaseService.addRelease(releaseDto);
}
ReleaseService:
@Service("ReleaseService")
public class ReleaseServiceImpl implements ReleaseService{
@Autowired
ReleaseRepository repository;
@Autowired
LabelRepository labelRepository;
@Override
public Release addRelease(ReleaseDto releaseDto) {
Release release = new Release();
Optional<Label> label = labelRepository.findById(releaseDto.getLabel_id());
if(label.isPresent()){
release.setName(releaseDto.getName());
release.setLabel(label.get());
repository.save(release);
}
return release;
}
I'm confused maybe i must implement an entity manager ?
Greetings.