Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

2K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda