1

I currently have Repository entity (lazy) in hibernate. When the controller is called, I want to open a session and load Repository before accessing repo.getIbId() because currently it throws LazyInitializationException. In my RepositoryDao class, I would like to load them before accessing ibid. Here are my couple approaches:

  1. Use Hibernate.initialize(Repository.class) in the service layer. To be honest, I am not sure how this because I read I need to have open session in order to do this.

  2. In my Dao layer, I can do sessionFactory.getCurrentSession() to open a session and then do something in here such as sessionFactory.getCurrentSession().get(Repository.class, ???) but not sure what to do here. Can I just do select * from Repository to load this entity?

Is there better way to handle this?

2
  • Why want you do this ? repo.getIbId() this specific to your entity ? Throws a LazyInitializationException, so try to solve this. Why want you to circumvent Hibernate ? Commented Aug 12, 2021 at 18:31
  • Because LazyInitializationException is caused by repo.getIbId() because Repository entity has not been loaded. Commented Aug 12, 2021 at 19:19

1 Answer 1

1

You need an active database connection / transaction to access a lazily loaded relation. In Spring this is done via the @Transactional annotation. Add this annotation to the method that is using your repository and loading the relation.

A couple notes:

  • put the @Transactional annotation close to the repository usage, e.g. not at the controller level
  • use @Transactional(readOnly = true) when you are just reading from the database as it has less of a performance penalty
Sign up to request clarification or add additional context in comments.

7 Comments

What if I don't have any active database connection (i.e. session is closed)? @Transactional doesn't work for me, I tried to put it as close to repo level.
You simply have to have a database connection :) You have to put the @Transactional at the method that is calling repo.getIbId() (which is probably in another class), not the respository class.
@Transactional only indicates to Spring that it should start a transaction when the method if invoked and commit or rollback on exiting the method. It sounds like something else is missing. Can you share how you configured your DataSource and EntityManager? Are they configured as Beans in a Configuration class, or in Spring Boot properties, or? Are you using a Spring Data Repository, or a Dao injecting the EntityManager, or?
I am not aware that lazy loading of relations is working without @Transactional above the repository that is fetching the base entity - care to share a link for that? As the base entity can be fetched I assumed the general database setup is working as intended.
Use of Spring's @Transactional is not required to use JPA's EntityManager to perform any action. You can use JPA without Spring.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.