每次启动应用程序spring Boot 时都会出现以下错误。
应用程序Favorite TO Start
产品描述:
com.base.model.AbstractDao中的字段会话需要一个类型为“org.hibernate.SessionFactory”的Bean,但找不到该Bean。
行动:
考虑在配置中定义一个类型为“org.hibernate.SessionFactory”的bean。
我添加了应用程序的实现:
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.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-web</artifactId>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Hibernate dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.3.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
字符串
application.property
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
型
配置类
@Configuration
@EnableTransactionManagement
@ComponentScan({"configure"})
@PropertySource({"classpath:application.properties"})
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory(){
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[]{"com.base","com.base.model"});
sessionFactory.setMappingResources(new String[]{"Employee.hbm.xml"});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hiberante.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
return properties;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.userName"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){
return hemf.getSessionFactory();
}
}
型
Employee.java
public class Employee implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String country;
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public void setCountry(String country){
this.country = country;
}
public String getCountry(){
return this.getCountry();
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", country="
+ country + "]";
}
}
型
Employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.base.model.Employee" table="Person">
<id name="id" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" not-null="true"></column>
</property>
<property name="country" type="java.lang.String">
<column name="country"></column>
</property>
</class>
</hibernate-mapping>
型
@Component
public class EmployeeDataDaoImpl {
@Autowired
SessionFactory sessionFactory;
public List<Employee> findAllEmployee(){
//// Criteria cri = getSession().createCriteria(Employee.class);
// List<Employee> dbList = cri.list();
// for (Employee employee : dbList) {
// System.out.println(employee.getCountry());
// }
return null;
}
}
型
我已经在stackoverflow上查找了相同的错误代码,但没有一个解决方案有效,因此再次将其与我的代码一起发布在这里。希望其他人可以指出我错在哪里。
3条答案
按热度按时间xxslljrj1#
对于初学者来说,有几件事与您的配置
1.混合来自不同Spring和Hibernate版本的jar
1.也可能已经管理依赖项
1.试着更聪明,然后Spring Boot 。
对于1.和2.只需删除
spring-orm
的<version>
标记以及hibernate-core
和hibernate-entitymanager
管理器依赖项。Sping Boot 已经在管理这些依赖项。您实际上可以删除所有已经由starter拉入的org.springframework
依赖项(实际上还有hibernate依赖项)。字符串
接下来在你的配置中,你至少配置了2个
SessionFactory
。我建议使用注解来定义你的实体,而不是hbm.xml
文件。型
当使用JPA注解时,Hibernate会自动检测你的实体(特别是与Sping Boot 相结合),这使得它非常强大。当然,你现在可以删除你的
Employee.hbm.xml
。接下来,我强烈建议你使用普通的JPA而不是普通的Hibernate。一般来说,这已经足够你使用了。
型
有了这个设置,你基本上可以完全删除你的
HibernateConfiguration
。是的,你可以,因为Sping Boot 检测到Hibernate并自动创建一个JpaTransactionManager
,启用transactions并预配置一个EntityManagerFactory
。如果你真的想在
SessionFactory
上使用普通的hibernate,只需要使用HibernateJpaSessionFactoryBean
来公开EntityManagerFactory
的底层SessionFactory
。型
然而,正如前面提到的,我强烈建议使用普通的JPA,因为它更容易设置,而且在JPA的当前状态下,它提供的功能几乎和普通的Hibernate一样多。
spring-boot-starter-data-jpa
,这意味着您依赖于Spring Data JPA。如果您使用JPA,这将使事情变得更容易。您可以删除EmployeeDataDaoImpl
,只需创建一个接口并使用它。型
也就是说,所有的CRUD方法(
findOne
,findAll
,save
等)都是为您提供的,而无需您创建实现。t3irkdon2#
您的SessionFactory配置是错误的。从您的pom.xml中,我看到您使用的是hibernate版本5,因此您的配置应该是:
字符串
而且你的pom.xml中有不同版本的hibernate jar,试着使用相同的版本。
7y4bm7vi3#
在我的例子中,我修复了Main-Class中错误的类名:
路径:资源->元信息->清单.NF