Hello
I am new to spring-dm server and osgi. I want to use hibernate criteria api for accessing the db. I am playing with greenpages and I modified its jpa-solution bundle to use hibernate instead of eclipse-link.
The problem is that Session is closed exception is raised after querying http://localhost:8080/greenpages/app/search.htm?query=rod.
I've tried to change the transaction management, i used compile-time weaving, tried to change transaction mode into proxy, finally i did my own debugging and discovered that session is closed because entity manager is closed.
Except entityManager.getSession().createCriteria(), calls like entityManager.find(...) work.
What's wrong?
Changed files are attached. To apply my changes just override files in the greenpages-2.0.0.RELEASE directory with files from the zip.
Stack trace:
spring-module.xml (ommited header)Code:org.hibernate.SessionException: Session is closed! org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49) org.hibernate.impl.SessionImpl.createCriteria(SessionImpl.java:1508) greenpages.jpa.JpaDirectory.search(JpaDirectory.java:62) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597) org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307) org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.doInvoke(ServiceInvoker.java:58) org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.invoke(ServiceInvoker.java:62) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131) org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) org.springframework.osgi.service.util.internal.aop.ServiceTCCLInterceptor.invokeUnprivileged(ServiceTCCLInterceptor.java:56) org.springframework.osgi.service.util.internal.aop.ServiceTCCLInterceptor.invoke(ServiceTCCLInterceptor.java:39) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) org.springframework.osgi.service.importer.support.LocalBundleContextAdvice.invoke(LocalBundleContextAdvice.java:59) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131) org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) $Proxy149.search(Unknown Source) greenpages.web.GreenPagesController.search(GreenPagesController.java:58) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:632) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:150) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:360) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:348) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:763) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:709) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:614) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:526) javax.servlet.http.HttpServlet.service(HttpServlet.java:690) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
template.mfCode:<!-- JPA EntityManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource"> <property name="jpaVendorAdapter"> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:databasePlatform="org.hibernate.dialect.H2Dialect" p:showSql="true"/> </property> </bean> <!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"/> <!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= --> <!-- Activates various annotations to be detected in bean classes: Spring's @Required and @Autowired, as well as JSR 250's @PostConstruct, @PreDestroy and @Resource (if available) and JPA's @PersistenceContext and @PersistenceUnit (if available). --> <context:annotation-config/> <!-- Instruct Spring to perform declarative transaction management automatically on annotated classes. --> <tx:annotation-driven mode="aspectj"/> <!-- Post-processor to perform exception translation on @Repository classes (from native exceptions such as JPA PersistenceExceptions to Spring's DataAccessException hierarchy). --> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> <!-- Will automatically be transactional due to @Transactional. EntityManager will be auto-injected due to @PersistenceContext. PersistenceExceptions will be auto-translated due to @Repository. --> <bean id="directory" class="greenpages.jpa.JpaDirectory"/>
JpaDirectory.javaCode:Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: GreenPages JPA Bundle-SymbolicName: greenpages.jpa Bundle-Vendor: SpringSource Inc. Bundle-Version: 2.0 Import-Template: org.springframework.*;version="[3.0, 3.1)", greenpages;version="[2.0, 2.1)", javax.persistence;version="[1.0.0, 1.0.0]", org.apache.commons.dbcp.*;version="[1.2.2.osgi, 1.2.2.osgi]", javax.sql;version="0", org.hibernate.*;version="[3.2, 3.5)" Import-Package: org.springframework.context.weaving;version="[3.0, 3.1)", org.springframework.transaction.aspectj;version="[3.0, 3.1)" Excluded-Exports: greenpages.jpa Excluded-Imports: org.springframework.test.*, org.junit.* Import-Library: org.hibernate.ejb;version="[3.3.2.GA,3.3.2.GA]"
Dependency to org.hibernate.ejb-library was added into pom.xml.Code:@Transactional @Repository final class JpaDirectory implements Directory { private static final String SEARCH_QUERY = "select l from Listing l where upper(l.lastName) like :term"; /** * Spring will inject a managed JPA {@link EntityManager} into this field. */ @PersistenceContext private HibernateEntityManager em; public Listing findListing(int id) { return em.find(JpaListing.class, id); } @SuppressWarnings("unchecked") public List<Listing> search(String term) { Session session = em.getSession(); Criteria crit = session.createCriteria(JpaListing.class); //Session is closed! exception crit.add(Restrictions.like("lastName", term)); return crit.list(); // return em.createQuery(SEARCH_QUERY).setParameter("term", "%" + term.toUpperCase() + "%").getResultList(); } }


