5

After upgrading my web application from spring boot 1.5.10 to spring boot 2.0.0 I ran into some runtime exceptions when accessing single entities.

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

My Repository:

public interface MyRepository extends JpaRepository<MyEntity, Long> {
}

In one of my services I called in spring data 1.x myRepository.findOne(id); to fetch a single entity. But the method findOne(Long id) was removed in spring data 2 so I had to switch to another method.

I found JpaRepository#getOne(ID id); After switching findOne to getOne all tests passed and were green. But during runtime I received the above mentioned exception. org.hibernate.LazyInitializationException

So I searched again for suitable methods and found CrudRepository#findById(ID id)

This time all worked out and I am happy, but the question is now, why do I get the org.hibernate.LazyInitializationException when using getOne() ?

mrkernelpanic
  • 4,268
  • 4
  • 28
  • 52
  • you get **org.hibernate.LazyInitializationException** due the fact that you try to access entity fields after session termination – gstackoverflow Sep 28 '18 at 12:57
  • @gstackoverflow Where is the transaction boundary? I don't see it in the logs – Paul A. Trzyna Feb 15 '19 at 16:49
  • @gstackoverflow What is the actual difference between the two. My understanding is that getOne returns a proxy, does findById not do that? Is the actual instance of the object returned populated with all the values? What happened to the findOne method? – Paul A. Trzyna Feb 15 '19 at 17:01

0 Answers0