1
我试图加载我的服务类的init方法从DB一些数据,但是当我称之为“getResultList()”方法,那么它抛出一个异常“会议闭幕”。getResultList()的初始化方法给错误“会被关闭”
我的applicationContext.xml
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="testService" class="com.impl.TestServiceImpl" init-method="init" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
我的服务类:
public Class TestServiceImpl implements TestService {
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public void init() {
Query query = entityManager.createQuery("from myTable");
query.getResultList(); // this causes error...
}
}
这是错误消息:
SEVERE: Exception sending context initialized event to listener instance of class
org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'testService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]:
Invocation of init method failed; nested exception is
javax.persistence.PersistenceException: org.hibernate.SessionException: Session is
closed!
Caused by: javax.persistence.PersistenceException: org.hibernate.SessionException:
Session is closed!
at
org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630)
所以我在做什么错在这里?我该如何解决这个问题?谢谢。
感谢您的回答,调用一些其他bean的业务方法奏效了,通过我已经有@Transactional服务类的方式(忘了把它放在这个问题),但是你如果是甚至有提到它不会奏效。 – Rizwan