我有一個SpringBoot
應用程序使用SpringData
來保存Oracle數據庫上的數據,並使用spring-boot-starter-activemq
來處理JMS隊列。如何將Hibernate會話關聯到JmsListener方法
我在公開一個DELETE
Rest WebService,這需要一段時間。而且我不希望我的用戶掛在回覆中。
因此我添加了一個註釋爲@JmsListener
的方法,它可以完成這項工作,我用jmsTemplate.convertAndSend()
來調用它。
但如果我的治療findAll()
部分作品完美,只要我試圖訪問數據(只是顯示它爲例)與findAll()
方法我面對我的懶惰集合LazyLoadingException
檢索,說我沒有休眠會話。 org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session
這裏是我的代碼片段是否有幫助:
public void myMethod(int batchSize) {
// Send a JMS message with a POJO
LOGGER.trace("Calling JMS method...");
final JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
jmsTemplate.convertAndSend("runJob", batchSize);
}
@JmsListener(destination = "runJob")
private void runJob(final int batchSize) {
LOGGER.debug("Calling runJob with batchSize {}", batchSize);
List<MyEntity> myEntities = myRepository.findAll();
LOGGER.debug("{} entities retrieved from the DB", myEntities.size()); // Prints the actual number of entities in my DB
for(Entity entity : entities){
LOGGER.debug("Entity name {}", entity.getName()); // Prints entity name
LOGGER.debug("Entity first collection's value {}", entity.getMyList().get(0).toString()); // org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session
}
}
// -----------------------
// Full code of my repository -> The implementation is generated by spring-data http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
public interface MyRepository extends org.springframework.data.jpa.repository.JpaRepository<MyEntity, Long>{}
我只是想知道如何讓我的會話連接到我的Jms方法。
乾杯, 奧利維爾
不要描述你的代碼和你的例外。發佈它們。 –
@JBNizet剛剛添加了一段代碼片段。順便說一句,這個異常在我的初始文章(懶惰集合中的LazyLoadingException)中列出,代碼(顯示數據)也在那裏。我雖然可以專注於真正重要的東西:問題而不是純粹的代碼。任何方式,在這裏。 – Olivier