JPA(Hibernate)+Spring : no session or session was closed !请注意是非J2EE(web)下如何解决!如果是web项目,配置
<filter>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
        <init-param>   
           <param-name>entityManagerFactory</param-name>   
           <param-value>entityManagerFactory</param-value>   
        </init-param> 
</filter>或者   <bean name="openSessionInViewInterceptor" class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">       
<property name="entityManagerFactory">       
<ref bean="entityManagerFactory"/>       
</property>       
   </bean>         <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="vicky"/>
   </bean>
可能我小白,因为我怎么看,上面的2种解决方案,只能解决J2EE下的(也就是web项目)...我现在只是J2SE...也就是说没有web.xml来配置Filter.也没有
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">       
<property name="interceptors">       
<list>       
<ref bean="openSessionInViewInterceptor"/>       
</list>       
</property>       
<property name="mappings">         
</property>因为J2SE没哪儿来的servlet!!!
请问,如何解决...

解决方案 »

  1.   

    我觉得不可能解决,spring必须是在web项目中应用的,为什么,因为它首先注册的是web应用的applicationContext,你的J2SE应用连上下文都没有,怎么能加载spring
      

  2.   

    如果单是hibernate的话,还可以。
      

  3.   

    将取出来的对象用
    Hibernate.initialize(object)加载吧
      

  4.   


    上下文也是一个对象,怎么只有web中才有呢?不是web程序也能加载spring文件吧
      

  5.   


    j2se可以加载spring啊...ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");