Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 767
DMine Avatar asked Nov 17 '20 16:11

DMine


2 Answers

@EqualsAndHashCode.Exclude
@ToString.Exclude

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

like image 119
Raghunath Avatar answered Oct 06 '22 00:10

Raghunath


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).

like image 27
Thomas Avatar answered Oct 06 '22 00:10

Thomas