6

I am running into the org.hibernate.LazyInitializationException error when I try to access a lazy loaded exception when excecuting the following:

@Transactional
public void displayAddresses()
{
     Person person = getPersonByID(1234);
     List<Address> addresses = person.getAddresses(); // Exception Thrown Here

     for(Address address : addresses)
          System.out.println(address.getFullAddress());
}

My entities look like this:

@Entity
@Table("PERSON_TBL")
public class Person
{
     ...

     @OneToMany(cascade=CascadeType.ALL, targetEntity=Address.class, mappedBy="person")
     private List<Address> addresses;

     ...
}

@Entity
@Table("ADDRESS_TBL")
public class Address
{
     ...

     @ManyToOne(targetEntity=Person.class)
     @JoinColumn(name="PERSON_ID", referencedColumnName="PERSON_ID")
     Person person;

     ...
}

I was under the impression that by using the @Transactional annotation in my displayAddresses() method, it would keep the session alive until the method was completed, allowing me to access the lazy-loaded Address collection.

Am I missing something?

EDIT

In accordance with Tomasz's advice: Within my displayAddresses() method, the status of TransactionSynchronizationManager.isActualTransactionActive(), turned out to be false.

That does narrow down the problem, but why would my Transaction not be active at this point?

4
  • 3
    Can you call TransactionSynchronizationManager.isActualTransactionActive()? It should return true. Otherwise @Transasctional is not working. Commented Feb 16, 2012 at 20:01
  • @TomaszNurkiewicz I edited my original post to include more information based on your suggestion. Commented Feb 16, 2012 at 20:12
  • 1
    Do you have <tx:annotation-driven/> in your configuration? Is your displayAddresses() method in a Spring-managed bean? Do you call this method from outside or from other transactional method? Commented Feb 16, 2012 at 20:15
  • How do you invoke the displayAddresses() method - from a method of the same bean? Commented Feb 16, 2012 at 21:26

2 Answers 2

5

This is already answered, but just wanted to post an alternative answer which worked for me in a similar case - for future generations ;)

In my case the issue was caused by the fact that the instance which contained the lazy-loaded collection had been manually evicted (all within transaction boundaries).

Sign up to request clarification or add additional context in comments.

1 Comment

It wasn't like that in my case, but it got me thinking - and it turned out that at one point an entity was cached (using ehcache) instead of dto - make sure that you are not doing it either
0

Having <tx:annotation-driven /> in my configuration file and using a Spring-managed version of my service class (to call the displayAddresses() method) did the trick.

Comments

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.