Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

2K
Views
Spring Framework Collection was evicted

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

@EqualsAndHashCode.Exclude
@ToString.Exclude

I have added these two annotations for @onetomany object and my error got vanished. But I don't know how.

over 4 years ago · Santiago Trujillo Report

0

As a first proposal, I would recommend to change this line:

release = repository.save(release)

The save method returns an updated object of the entity, so by returning the "non-updated" one, there may be problems when trying to serialize the response.

Following our discussion, probably removing CascadeType.ALL was the issue here - Hibernate tried to do some magic with the related entities, which were out of sync.

Maybe you also want to create a DTO for the Response, as it's good practice to separate this from the entity.

Using FetchType.EAGER is generally also not a good idea - some notes on this https://vladmihalcea.com/eager-fetching-is-a-code-smell/ . If you change this, you probably need to fetch the related entities manually in the service - changing to a separate DTO and this isn't an issue anyway.

You may check out https://bootify.io, you can create an example REST API (but not with your custom endpoint anyway).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!