I have a class annotated with @Service that is implementing javax.jms.MessageListener, inside the onMessage function i am retrieving an object from a repository, but when i try to preload the lazy loaded properties i get the "could not initialize proxy - no Session" exception.
I prefer not to disable lazy loading as the page entity gets really big with a lot of sub entities and most of the times i don't need these sub entities while working with the page object.
I found and tried a lot of other questions and solutions on google but none of them seem to make a difference for me.
@Service
public class PageService implements MessageListener {
private PageRepository pageRepository;
public PageService(PageRepository pageRepository){
this.pageRepository = pageRepository;
}
@Override
@Transactional
public void onMessage(Message message) {
// Message handling etc etc..
int id = ....
Page page = pageRepository.findOne(id); // Page is retrieved successfully
page.InitializeAll(); // <--- Error happens here
// Build response message.
}
}
Entity:
@Entity
public class Page {
....
@OrderBy("id ASC")
@OneToMany(mappedBy = "page", cascade = CascadeType.ALL)
public Set<Row> rows;
public void InitializeAll(){
Hibernate.initialize(this.rows); // <--- Error
rows.forEach(r -> r.InitializeAll());
}
}
Page repository is just extending CrudRepository:
public interface PageRepository extends CrudRepository<Page, Integer>
in another file in the same package(/folder) i have another function annotated with @Transactional with the same function calls inside and here it's all working fine.
@MessageMapping("/page/{id}/request")
@SendToUser(value = "/queue/private", broadcast = false)
@Transactional
public initialPageMessage subscribeClient(@DestinationVariable("id") int pageId) {
initialPageMessage resp = new initialPageMessage();
resp.page = pageRepository.findOne(pageId);
resp.page.InitializeAll();
return resp;
}
TransactionManagerConfiguration:
@Configuration
@EnableTransactionManagement
public class TransactionManagerConfiguration {
private EntityManagerFactory entityManagerFactory;
private DataSource datasource;
public TransactionManagerConfiguration(EntityManagerFactory entityManagerFactory, DataSource datasource){
this.entityManagerFactory = entityManagerFactory;
this.datasource = datasource;
}
@Bean
public PlatformTransactionManager transactionManager(){
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(entityManagerFactory);
tm.setDataSource(datasource);
return tm;
}
}
Stacktrace
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: models.Page.rows, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:204) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:566) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:739) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.Hibernate.initialize(Hibernate.java:65) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at models.Page.InitializeAll(Page.java:31) ~[classes/:na]
at controllers.PageService.processMessage(PageService.java:93) ~[classes/:na]
at controllers.PageService.onMessage(PageService.java:75) ~[classes/:na]
at org.apache.activemq.ActiveMQMessageConsumer.dispatch(ActiveMQMessageConsumer.java:1401) ~[activemq-client-5.14.3.jar:5.14.3]
at org.apache.activemq.ActiveMQSessionExecutor.dispatch(ActiveMQSessionExecutor.java:131) [activemq-client-5.14.3.jar:5.14.3]
at org.apache.activemq.ActiveMQSessionExecutor.iterate(ActiveMQSessionExecutor.java:202) [activemq-client-5.14.3.jar:5.14.3]
at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:133) [activemq-client-5.14.3.jar:5.14.3]
at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:48) [activemq-client-5.14.3.jar:5.14.3]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_40]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_40]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_40]
After sometime i found the issue, the problem was that the 'onMessage' method was called from inside the same class. This prevented the @Transactional proxy (wrapping the class) from being called. When i moved the @Transactional to another class and called the function from outside that class it started to work.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments