【问题标题】:How to implement Spring boot + Hibernate如何实现 Spring Boot + Hibernate
【发布时间】:2020-06-01 03:55:06
【问题描述】:

我收到此错误:

*************************** 
APPLICATION FAILED TO START
***************************

Description:

Field sessionFactory in com.demo.dao.EmployeeDAO required a bean of type 'org.hibernate.SessionFactory' that could not be found.

Action: Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration.

我的HibernateUtil 班级是:

@Configuration
public class HibernateUtil {

    @Autowired
    private EntityManagerFactory factory;

    @Bean
    public SessionFactory getSessionFactory() {
        if(factory.unwrap(SessionFactory.class) == null) {
            throw new NullPointerException("Factory is not a hibernate factory.");
        }
        return factory.unwrap(SessionFactory.class);
    }
}

我的EmployeeDao 班级是:

@Repository
public class EmployeeDAO {

    @Autowired
    private SessionFactory sessionFactory;
    
    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }
    
    public void save(Employee emp) {
        
        Session session = null;
        
        try {
            session = sessionFactory.openSession();
            System.out.println("Session got.");
            Transaction tx = session.beginTransaction();
            session.save(emp);
            tx.commit();
        }catch(HibernateException he) {
            he.printStackTrace();
        }
    }
}

application.properties 文件,

spring.mvc.view.prefix=/pages/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://localhost:3306/manissh
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

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

【问题讨论】:

  • 请发布 .property 文件
  • 这是你唯一的错误吗?您似乎正在创建 SessionFactory bean。但也许创建该 bean 时出错?就像未能创建 EntityManagerFactory 一样?
  • 在 getSessionFactory() 中放一条日志语句或放一个断点,以调试模式启动应用程序。我很确定永远不会调用该方法。当配置类不在 Spring 的默认类路径扫描机制将要查找的位置时,可能会发生这种情况。

标签: java spring spring-boot spring-data-jpa


【解决方案1】:

尝试在您的存储库中创建Autowired sessionFactory

   @Repository
   public class EmployeeDAO {
            private SessionFactory sessionFactory;

            @Autowired
            public EmployeeDAO(EntityManagerFactory entityManagerFactory) {
                    this.sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
            }
    }

或试试这个解决方案 - https://stackoverflow.com/a/43895827/6582610

【讨论】:

  • 我只想在 EmployeeDAO 中处理“保存”方法。并且只想将 EntityManagerFactory 保留在 HibernateUtil 类中。这样我就可以在各种类中使用“会话工厂”。
  • 为什么这个解决方案会导致循环依赖?它创建了一个实体管理器,但自动装配了一个循环依赖错误:S
【解决方案2】:

您无需创建会话。只需在 pom.xml 中添加依赖 spring-boot-starter-jpa 即可使用 hibernate。 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>

如果你这样做了,那么你可以直接在存储库中给出查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    相关资源
    最近更新 更多