2017-04-21 57 views
4

每次启动应用程序弹簧启动时,我都会收到错误信息。需要一个无法找到的'org.hibernate.SessionFactory'类型的bean


应用程序未能启动


说明:com.base.model.AbstractDao

现场会话所需类型的 'org.hibernate.SessionFactory实例' 一豆,可能不被发现。

操作:

考虑您的配置定义类型 '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> 

EmployeeDaoImpl

@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上同样的错误代码,但没有任何解决方案工作,因此再次发布它与我的代码。 希望别人能指出我出错的地方。

回答

7

对于初学者有一对夫妇的事情与你的配置

  1. 从不同春混合罐和Hibernate版本
  2. 也可能已经成功依赖
  3. 为了做个聪明的话,Spring启动。

For 1. and 2。只需删除spring-orm<version>标签和hibernate-corehibernate-entitymanager管理器依赖关系。 Spring Boot已经在管理这些。实际上你可以删除那些已经被初学者拉入的org.springframework依赖关系(实际上也是休眠类)。

<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-test</artifactId> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
    </dependency> 
</dependencies> 

下一步在配置您配置了至少2 SessionFactory的。我会建议使用注释来定义您的实体而不是hbm.xml文件。

@Entity 
@Table("person") 
public class Employee implements Serializable{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue 
    private int id; 

    @Column(nullable=false) 
    private String name; 

    private String country; 

} 

当使用JPA注解Hibernate会自动检测您的实体(尤其是春季引导相结合),这使得它非常强大。当然,你现在可以删除你的Employee.hbm.xml

Next EmployeeDataDaoImpl我强烈建议使用普通的JPA而不是普通的Hibernate。一般来说,这为您提供了足够的帮助。

@Repository 
public class EmployeeDataDaoImpl { 


    @PersistenceContext 
    private EntityManager entityManger; 


    public List<Employee> findAllEmployee(){ 
     return em.createQuery("select e from Employee e", Employee.class).getResultList(); 
    } 
} 

有了这个设置你基本上可以完全删除您HibernateConfiguration。是的,您可以像Spring Boot检测到休眠并自动创建JpaTransactionManager,启用事务并预先配置EntityManagerFactory

如果你真的想用SessionFactory使用普通休眠,只需使用HibernateJpaSessionFactoryBean来暴露EntityManagerFactory的底层SessionFactory

@Bean 
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) { 
    HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean(); 
    factory.setEntityManagerFactory(emf); 
    return factory; 
} 

然而如前所述,我会强烈建议使用纯JPA,因为这是比较容易设置了很多与JPA的当前状态,它提供了几乎同样多的功能直接使用Hibernate一样。

专业提示 您对spring-boot-starter-data-jpa的依赖,这意味着你正在对春数据JPA的依赖。如果你使用JPA,这会让事情变得更容易。你可以删除你的EmployeeDataDaoImpl,只需创建一个界面并使用它。

public interface EmployeeRepository extends JpaRepository<Employee, Long> {} 

就是它,提供所有的CRUD方法(findOnefindAllsave等)为您无需您创建一个实现。

+0

感谢您的回复。它对我很有帮助。 –

+0

如果它有助于消除错误(和/或改善您的代码),请接受它作为答案和投票。 –

+0

从哪里我可以接受作为答案和投票。 –

2

您的SessionFactory配置错误。从你的pom.xml我看你使用Hibernate版本5,所以你的配置应该是:

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext 

,你也有不同版本的Hibernate罐子在你的pom.xml,尽量使用相同的版本。

+0

感谢您的回复,但问题仍然存在 –

相关问题