I have a spring data rest application with relation between types Match and Round
@Entity
public class Match {
@OneToMany
private List<Round> rounds;
...
}
When a link is created between a match and a round, e.g. like this
curl -X PUT -d "http://localhost:8080/rounds/2" -H "Content-Type:text/uri-list" http://localhost:8080/matches/1/rounds;
i capture this with an EventHandler to do some updates on my domain model:
public class MatchEventHandler
@HandleAfterLinkSave
public void handleLinkSave(Match match, List<Round> rounds) {
...
}
I need to access the second argument in order to do my update but doing so, e.g. with rounds.get(0), returns
org.hibernate.LazyInitializationException: failed to lazily initialize a collection, could not initialize proxy - no Session
Reading other threads about Hibernates LazyInitializationExceptions i see mainly three approaches that are suggested:
- Use Hibernate.initialize() - i don't see on which method i could call this and it would add an ugly dependency to Hibernate framework
- Put controller method into a transaction - i understand that spring data already puts everything into a transaction. Also as this is a spring data rest application i'm not using any controller or service layer and therfore i would not know what exactly to put into a transaction.
- Set FetchType.EAGER on the relation - although not really a valid solution i did try it. In that case the second argument of the @HandleAfterLinkSave method is an empty list, so it also does not deliver the expected result.