我有一个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