- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我面临一个非常奇怪的问题。我正在使用 Spring MVC 3.0+Spring Security 3.2+hibernate 4.3。在登录之前,我可以在登录之前访问数据库并获取实体。但是通过 spring security 登录后,我收到异常 org.hibernate.HibernateException: No Session found for current thread
。 sessionFactory.getCurrentSession()
上抛出此异常
我使用了 AOP 的横切管理。我的问题是:
为什么登录后出现异常?登录前工作正常吗?
我的代码如下:
@Repository("categoryDAO")
public class CategoryDAOHibernateImpl implements CategoryDAO {
@Autowired
SessionFactory sessionFactory;
public List<Category> findAll() {
List<Category> list;
try {
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Category.class);
list = criteria.list();
} catch (Exception e) {
throw new BookStoreDAORuntimeException(e.getMessage(), e);
}
return list;
}
}
完整堆栈跟踪:
StandardWrapperValve[appServlet]: Servlet.service() for servlet appServlet threw exception
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1012)
at com.abhendra.bookstore.common.entities.dao.hibernate.UserDAOHibernateImpl.findByEmail(UserDAOHibernateImpl.java:68)
at com.abhendra.bookstore.management.user.impl.UserManagementServiceImpl.findByEmail(UserManagementServiceImpl.java:20)
at com.abhendra.bookstore.LibreryController.showCategory(LibreryController.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
]]
daoContext.xml
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>jdbc:postgresql://localhost:5432/bookstore</value>
</property>
<property name="username">
<value>bookstore</value>
</property>
<property name="password">
<value>bookstore</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>Author.hbm.xml</value>
<value>Book.hbm.xml</value>
<value>Category.hbm.xml</value>
<value>Comment.hbm.xml</value>
<value>Download.hbm.xml</value>
<value>Rating.hbm.xml</value>
<value>Role.hbm.xml</value>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
jdbc.use_getGeneratedKeys=true
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.generate_statistics=false
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false
hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<tx:advice id="defaultTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<tx:annotation-driven transaction-manager="transactionManager"/>
<aop:config>
<aop:pointcut id="dbServiceOperations"
expression="execution(* com.abhendra.bookstore.common.entities.dao.*DAO*.*(..))" />
<aop:advisor pointcut-ref="dbServiceOperations" advice-ref="defaultTxAdvice" />
</aop:config>
安全配置:
<security:http auto-config="false" use-expressions="true">
<security:form-login login-page="/bookstore/authentication/login"
login-processing-url="/bookstore/authentication/loginProcess"
default-target-url="/bookstore/home/index"
authentication-failure-url="/bookstore/authentication/login?login_error=1" />
<!-- authentication-failure-handler-ref="postFailedAuthHandler" -->
<security:logout logout-url="/bookstore/authentication/logout"
logout-success-url="/bookstore/authentication/login" />
<security:intercept-url pattern="/bookstore/admin/**" access="isAuthenticated()" />
<security:intercept-url pattern="/bookstore/home/**" access="isAuthenticated()" />
</security:http>
<bean id="authenticationProvider" class="com.abhendra.core.spring.security.BookstoreAuthenticationProvider" />
<security:authentication-manager alias="authenticationManager" erase-credentials="false">
<security:authentication-provider ref="authenticationProvider"></security:authentication-provider>
</security:authentication-manager>
rootContext.xml
<import resource="classpath:daoContext.xml" />
<context:component-scan base-package="com.abhendra.bookstore, com.abhendra.core" />
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/security-config.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/bookstore/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- Map all /resources requests to the Resource Servlet for handling -->
<!-- <servlet-mapping>
<servlet-name>Resources Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping> -->
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/bookstore/*</url-pattern>
</servlet-mapping>
提前致谢。:-)
最佳答案
代码尝试获取当前 session ,但 @Transactional 没有配置允许容器在调用带注释的方法之前将 session 添加到线程。
在堆栈跟踪中,我们可以看到 LibreryController 调用 UserManagementServiceImpl,后者又调用 UserDAOHibernateImpl,并且中间没有事务代理。
您应该添加对@Transactional的支持,用它注释UserManagementServiceImpl,这应该可以解决问题。看这个blog post了解如何执行此操作。
关于java - org.hibernate.HibernateException : No Session found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21761757/
我正在做这个项目,其中我有一个用 Java (HttpServlet) 完成的 Web 服务,它使用 Hibernate 选择、插入和更新信息到数据库中,并将对象数组作为 JSON 返回给连接到 JS
我尝试同时修改大约 14 个 REST 实体。 前几次修改是成功的(以及中间的一些修改),其他一些修改偶尔会失败,出现 HibernateException。 这是我的 servlet: public
假设我们有一些看起来像这样的实体(我会尽量保持简单): @Entity class MainEntity { //nothing too special here } @Entity class
我有这个项目设置,我正在尝试使用 JPA 和 Hibernate 作为提供程序来将数据存储在我的本地 h2 数据库中。它全部配置为我的 spring 项目的一部分,我使用 tomcat 作为我的服务器
我正在尝试测试 hibernate 在我的项目中是否工作,因为我正在尝试创建 session ,IDEA 告诉我 org.hibernate.HibernateException 和 java.lan
我正在使用带有基本 hibernate 程序的 MySQL Workbench 5.2 CE 数据库在运行时它给我错误: Caused by: org.hibernate.HibernateExcep
我有一个执行以下步骤的应用程序: 在 session 中放置对象: def product = Product.get(1) session["product"] = product 执行 Ajax
我在 Hibernate 和 Spring 开发方面绝对是新手,我有以下问题 我有一个名为 KM_ProjectInfoStatus 的实体类,它映射一个名为 KM_PROJECT_INFO_STAT
我不断收到此错误...使用 appach tomcat 的 Eclips IDE 根本原因 java.lang.ClassNotFoundException: org.hibernate.Hibern
我是 Hibernate 的新手。我创建了一个简单的程序来使用 Hibernate 将值插入数据库。 我的文件结构是这样的 src -> com.visolve -> AddStudent.java
嗨,我面临一个非常奇怪的问题。我正在使用 Spring MVC 3.0+Spring Security 3.2+hibernate 4.3。在登录之前,我可以在登录之前访问数据库并获取实体。但是通过
我想使用审核数据库插件将构建信息记录到数据库中。我已正确安装插件并使用以下配置: JDBC Driver class: org.hsqldb.jdbc.JDBCDriver** JDBC Url:jd
我在我的 Web 应用程序中使用 Spring Transactional 和 AspectJ。 应用程序上下文.xml
我目前使用confluence和mysql制作个人wiki。但是我有 500 异常 HibernateException。 汇合日志: Caused by: com.atlassian.conflue
这个问题在这里已经有了答案: java.lang.ClassNotFoundException: org.hibernate.HibernateException (5 个答案) 关闭 5 年前。
我是 Hibernate 的新手,在应用程序中定义 JPQL 查询时遇到以下问题。 所以我有以下情况: 1)我有这个模型KMCountryArea类,它被注释为将其映射到数据库表并定义了一个查询 @N
这是代码的最小版本,我花了很多时间才弄清楚为什么它不起作用: Query q = session.createQuery(queryString); q.setTimestamp(0, new jav
我试图删除数据库中不存在的实体,但 delete() 方法没有抛出任何异常。 当我试图删除一个不存在的实体时,如何得到一个错误? 我已经在下面复制了我的代码: public void remove(M
我收到此错误消息: error: Found shared references to a collection: Person.relatedPersons 当我尝试执行 addToRelatedP
完整的堆栈跟踪: org.hibernate.HibernateException: Could not instantiate resultclass: com.firstbankpr.lmu.da
我是一名优秀的程序员,十分优秀!