Loading lazy collection in @Transactional method

I try to load collection to my object (I cannot use FetchType.EAGER), but I am getting following exception:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Produkt.opinie, could not initialize proxy - no Session

I don't know why as I am doing it in @Transactional scope:

@Service
@Transactional
public class ProduktService extends AbstractService implements IProduktService {


    public Produkt findProduktById(Integer id) {
        Produkt produkt = (Produkt) getSessionFactory().getCurrentSession()
                .get(Produkt.class, id);
            produkt.getOpinie();
        return produkt;
    }
}

Answers


There could be many reason. For once having @Transactional annotation is not all it takes -- you need to check a transaction manager has been properly configured in your spring application context. Then you need to check your application has proper AOP support such that Spring can automatically start and finish transaction on methods annotated with @Transactional.

You also need a proper transaction manager, eg: in you case since you're using Hibernate, you need a HibernateTransactionManager configured.

It is also helpful to debug the actual SQL statement issued by Spring / Hibernate to check if you're transaction really works properly

Chapter 11 of the spring manual is a good reading for this topic.


Need Your Help

How to Change Default Virtualenvwrapper Prompt

python bash virtualenv command-prompt virtualenvwrapper

How do you change the default Virtualenvwrapper prompt? By default, working on a particular virtual environment with a command like "workon <_name_of_env_>" prepends the name of the virtualenv to

Is there any PHP/Java equivolent of the purely file-based MSSQL db of .net?

c# java php .net database

Am a big fan of the file-based .mdf db of .net which fits the bill just right in terms of portability and ease of implementation for small/medium projects and independent small-scale modules of lar...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.