Spring 4和Hibernate 4 Session错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 4和Hibernate 4 Session错误相关的知识,希望对你有一定的参考价值。
在我的Spring MVC和hibernate web项目什么是java base cofiguration,它很快就从Entity类生成表,但是当我试图从我的post表生成一个数据列表但是我输入了一个关于getCurrent Session的错误......
我的实体类:
@Entity
@Table(name="post")
public class Post{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(nullable = false)
String title;
@Column(nullable = false)
@Type(type="text")
String body;
@Column(nullable = true)
String feature_image;
@Column(nullable = true)
int active_status = 1;
}
DAO:
@Repository
public class TestDao {
@Autowired
private SessionFactory sessionFactoryBean;
private Session getCurrentSession() {
return sessionFactoryBean.getCurrentSession();
}
@SuppressWarnings("unchecked")
public List<Post> listProduct() {
return (List<Post>)
getCurrentSession().createCriteria(Post.class).list();
}
}
服务:
@Service
@Transactional
public class TestService{
@Autowired
private TestDao testDao;
public List<Post> postList(){
return testDao.listProduct();
}
}
控制器:
@Controller
public class HomeController {
@Autowired
TestService testService;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(ModelMap map) {
List<Post> results = testService.postList();
Post lastPost=null;
if(!results.isEmpty()){
Collections.reverse(results);
lastPost = results.get(0);
results.remove(0);
}
map.addAttribute("lastPost",lastPost);
map.addAttribute("postList",results);
return "home";
}
}
配置类:
@Configuration
@PropertySource("classpath:db.properties")
public class AppDataSourceConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/testdb?useSSL=false");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new
LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setPackagesToScan("com.imran.model");
sessionFactoryBean.setHibernateProperties(hibProperties());
return sessionFactoryBean;
}
private Properties hibProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect",
"org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.hbm2ddl.auto","update");
return properties;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new
HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
}
最后错误:
Mar 04, 2018 10:28:52 PM org.apache.catalina.core.StandardWrapperValve
invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path
[/BlogSolutionUsingSpringHibernateWithClassLevelConfig] threw exception
[Request processing failed; nested exception is
org.hibernate.HibernateException: Could not obtain transaction-
synchronized
Session for current thread] with root cause
org.hibernate.HibernateException: Could not obtain transaction-
synchronized Session for current thread at
org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSes sionContext.java:134)位于com.imran.dao.TestDao.getCurrentSession(TestDao.java)的org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014) :21)com.imran.dao.TestDao.listProduct(TestDao.java:27)at com.imran.service.TestService.postList(TestService.java:23)at com.imran.controller.HomeController.index(HomeController。 java:29)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位于org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod)的org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)中的.reflect.Method.invoke(Method.java:497) .java:136)org.springframework.web.servlet.mvc.method.annot位于org.springframework.web.servlet.mvc.method的org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871)的ation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)。在org.springframework.web.servle.DispatcherServlet.doDispatch(DispatcherServlet。)的org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)中的annotationMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777)。 java:991)org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)atg.springframework.web.servlet .FrameworkServlet.doGet(FrameworkServlet.java:870)位于org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet。)的javax.servlet.http.HttpServlet.service(HttpServlet.java:622)。 java:855)at ork.apache.atache.atvice.httpServ.device(HttpServlet.java:729)atg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)org.apache.catalina.core.ApplicationFilterChain .doFilter(ApplicationFilterChain.java:206)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)org.apache.catalina.core.StandardContextValve.invoke( StandardContextValve.java:106)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)atg.apache.catalina org.apache.catalina.valves.AbstractA上的.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)的org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)上的ccessLogValve.invoke(AbstractAccessLogValve.java:616) .apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:673)at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor .doRun(NioEndpoint.java:1500)atg.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)at at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run( Thread.java:745)
在类上添加@EnableTransactionManagement可以解决您的问题
以上是关于Spring 4和Hibernate 4 Session错误的主要内容,如果未能解决你的问题,请参考以下文章
升级到 hibernate 4 和 spring 4 jars 但仍然得到 org.hibernate.engine.FilterDefinition classnotfoundexception
Spring 4.1.6 和 Hibernate 3.2.2?
Spring 4.1.0.RELEASE 和 Hibernate 4.3.6.Final 的依赖关系问题