ssh 集成框架 dao层 如何使用session

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh 集成框架 dao层 如何使用session相关的知识,希望对你有一定的参考价值。

平台采用ssh集成框架,去掉了hibernate的配置文件,相关配置都集成在applicationContext.xml文件中,由于各种原因,想在action层直接使用
query查询或者Criteria,但需要创建session对象,请问使用什么方法,使用session呢?

让action继承HibernateDaoSupport这个类,就可以了,然后在spring的配置文件中添加对sessionFactory的属性依赖,试试吧,应该可以! 参考技术A 不要直接创建session,把dao像service一样注入到action里使用,dao获取session没问题吧?
注意事务就行了
参考技术B hibernateTeplet.getsseion()

SSH dao层异常 org.hibernate.HibernateException: No Session found for current thread

解决方法:

在 接口方法中添加 事务注解 即可。

 

 

public interface IBase<PK extends Serializable, T> {
    
    @Transactional
    void add(T t);
    
    @Transactional
    User get(PK id);
    
    @Transactional
    List<T> list();
    
    @Transactional
    void update(T t);
    
//    @Transactional
    void delete(PK id);
}

 

 

 

 

以下为异常原文信息:


 

org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:941)
at com.ht.dao.impl.UserDaoImpl.getSession(UserDaoImpl.java:49)
at com.ht.dao.impl.UserDaoImpl.get(UserDaoImpl.java:27)
at com.ht.dao.impl.UserDaoImpl.delete(UserDaoImpl.java:43)
at com.ht.dao.impl.UserDaoImpl.delete(UserDaoImpl.java:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
at com.sun.proxy.$Proxy20.delete(Unknown Source)
at com.ht.service.impl.UserServiceImpl.delete(UserServiceImpl.java:35)
at com.ht.service.impl.UserServiceImpl.delete(UserServiceImpl.java:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
at com.sun.proxy.$Proxy22.delete(Unknown Source)
at com.ht.test.HibernateTest.delete(HibernateTest.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)

 

@Transactional




















































以上是关于ssh 集成框架 dao层 如何使用session的主要内容,如果未能解决你的问题,请参考以下文章

SSH框架整合中Hibernate实现Dao层常用结构

SSH dao层异常 org.hibernate.HibernateException: No Session found for current thread

SSH框架中POJO层, Dao层,Service层, Action层的功能理解

SSH框架的集成优化

SSH集成(Struts+Spring+Hibernate)

SSH框架总结