我已经创建了一个Spring Boot应用程序,并且想要处理Hibernate SessionFactory,因此在我的服务类中,我可以像下面这样调用Hibernate SessionFactory:
@Autowired private SessionFactory sessionFactory;
我在stackoverflow中发现了类似的问题,我必须在application.properties中添加以下行:
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
但我收到此错误:
Cannot resolve property 'current_session_context_class' in java.lang.String
我该如何解决?
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
尝试HibernateJpaSessionFactoryBean在您的Spring配置中启用。
HibernateJpaSessionFactoryBean
@Bean public HibernateJpaSessionFactoryBean sessionFactory() { return new HibernateJpaSessionFactoryBean(); }
通过Spring配置,我的意思是用@Configuration注解或@SpringBootApplication(它用隐式注解@Configuration)进行注解的类。
@Configuration
@SpringBootApplication