问题描述
我是Spring Boot的新手.我正在尝试使用Hibernate制作Web应用程序,但遇到了这些错误.我看过类似的问题,但找不到解决方案.我该如何解决?
I am new on Spring Boot. I am trying make a web app using Hibernate but I am experiencing these errors. I looked similar questions but I couldn't find a solution. How can I solve it?
java.lang.ClassNotFoundException: org.hibernate.engine.transaction.spi.TransactionContext位于 java.net.URLClassLoader.findClass(URLClassLoader.java:381) 〜[na:1.8.0_91]在 java.lang.ClassLoader.loadClass(ClassLoader.java:424)〜[na:1.8.0_91] 在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331) 〜[na:1.8.0_91]在 java.lang.ClassLoader.loadClass(ClassLoader.java:357)〜[na:1.8.0_91] 在 org.springframework.orm.hibernate4.HibernateTransactionManager.isSameConnectionForEntireSession(HibernateTransactionManager.java:711) 〜[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]在 org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:445) 〜[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]在 org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) 〜[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]在 org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447) 〜[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]在 org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277) 〜[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]在 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 〜[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]在 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]在 com.sun.proxy.$ Proxy77.listPosts(未知来源)〜[na:na]在 com.monat.service.PostServiceImpl.listPosts(PostServiceImpl.java:24) 〜[classes/:na]在 com.monat.controller.PostController.list(PostController.java:20) 〜[classes/:na]
java.lang.ClassNotFoundException: org.hibernate.engine.transaction.spi.TransactionContext at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_91] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_91] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_91] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_91] at org.springframework.orm.hibernate4.HibernateTransactionManager.isSameConnectionForEntireSession(HibernateTransactionManager.java:711) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:445) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at com.sun.proxy.$Proxy77.listPosts(Unknown Source) ~[na:na] at com.monat.service.PostServiceImpl.listPosts(PostServiceImpl.java:24) ~[classes/:na] at com.monat.controller.PostController.list(PostController.java:20) ~[classes/:na]
我的pom.xml依赖项
My pom.xml dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
我的控制器类
@Controller
@RequestMapping("/posts")
public class PostController
{
@Autowired
PostService postService;
@RequestMapping(value="", method=RequestMethod.GET)
public String list(Model model){
model.addAttribute("listData",postService.listPosts());
return "list";
}
}
我的服务等级
@Service
public class PostServiceImpl implements PostService{
@Autowired
PostDao postDao;
@Override
public List<Post> listPosts() {
return postDao.listPosts();
}
}
我的存储库类
@Repository
public class PostDaoImpl implements PostDao{
@Autowired
private SessionFactory sessionFactory;
public PostDaoImpl(){}
public PostDaoImpl(SessionFactory sessionFactory){
this.sessionFactory=sessionFactory;
}
@Override
@Transactional
public List<Post> listPosts() {
@SuppressWarnings("unchecked")
List<Post> listPost2 = (List<Post>) sessionFactory.getCurrentSession().createQuery("from Post");
return listPost2;
}
}
推荐答案
我已经解决了.我已经删除了我的配置类的这一部分,它已经解决了.
I have solved it. I have removed this part my config class and it's solved.
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager =
new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
这篇关于Spring Boot-java.lang.ClassNotFoundException:org.hibernate.engine.transaction.spi.TransactionContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!