抱歉英语不好.我得到了我的项目spring-boot 1.1.8,Encountered Exception No CurrentSessionContext configured,然后我做了一些搜索,添加属性<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>可以解决这个问题,但是
如何使用java类配置此属性?
更新:我改为Hibernate4.x方式来定义SessionFactory但仍然遇到同样的错误,请帮忙!
使用java 1.8和speing boot 1.1.8
这是我的pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclipse.wtp.contextRoot>../</m2eclipse.wtp.contextRoot>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.1.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是我的服务和控制器
@AutoWried
SessionFactory sessionFactory;
@Service
@Transactional
public class UserServiceImpl implements UserService { …Run Code Online (Sandbox Code Playgroud) 我改变了我的应用程序以使用Hibernate EntityManager(来自Hibernate会话),
但我有一些旧代码(我无法更改这些代码),在下面的代码中使用:
getSessionFactory().getCurrentSession() .
我有sessionFactory的bean,所以上面的代码应该工作,但在运行时我有
HibernateException("No Session found for current thread"),甚至上面的代码是在事务块中执行的.
仅供参考:我检查了事务资源(在调试模式下)并且使用密钥EntityManagerFactory,会话存在但不在SessionFactory下