Hibernate LazyInitializationException During Blue Prism Training Use Case

Hi everyone,

I’m currently doing some hands-on practice as part of my Blue Prism Training and decided to experiment a bit with Hibernate for backend data handling in a sample Java project.


While testing some process automation scenarios, I keep running into a LazyInitializationException. It happens when I try to access a related entity after the session is closed, which I understand is expected—but I’m not sure of the best approach to fix it in a real-world scenario.

In this specific use case, I’m simulating a bot fetching customer data via a REST API, which hits a Hibernate-managed entity with lazy loading. I’ve read about DTO mapping and JOIN FETCH, but I’m unsure what’s best in a clean architecture setup.

Can someone point me in the right direction or share how they’d handle this in a production-grade project?

Really appreciate any help or shared experience!

Thanks,
HJ Bishop

Hello @Bishop, do you always know when the lazy association will need to be loaded? If that is the case, our suggestion is indeed to force eager fetching in those cases, using either join fetch or an EntityGraph, so that Hibernate can more efficiently retrieve the data you need within the load operation.

If, on the other hand, you want to preserve laziness, and only perform loading when really needed, you will need to keep the Hibernate session open until you’re sure no more data-access can happen.

You can read more about proxies and lazy fetching in this chapter of Hibernate’s introductory guide.