required a bean of type 'org.hibernate.SessionFactory' that could not be found.

我写了一个SpringBoot项目,当我在项目中使用

@Repository 
public class xxDaoImpl implements xxDao {
    @Autowired
    private SessionFactory session;
我得到了如下错误

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

Description:

Field session in xx.dao.ParkingSpaceDaoImpl 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.
所以我找到了一个适合我的解决方案。正如您正在使用.xml配置文件,这样写可能是对的。但是对于那些正在使用application.properties文件的用户,您需要将此行添加到配置类或主应用程序类中。
@Bean  
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){  
    return hemf.getSessionFactory();  
}   

或者

@Bean
    public HibernateJpaSessionFactoryBean sessionFactory() {
        return new HibernateJpaSessionFactoryBean();
    }


完成后将下面的内容添加到application.properties配置文件中

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
这样配置后,将成功启动项目,并成功创建sessionFactory,如果您也遇到了同样的问题,希望能帮助到你。







  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要添加org.hibernate程序包,你需要执行以下步骤: 1. 下载Hibernate 2. 将Hibernate的jar文件添加到你的项目中 3. 在你的代码中导入所需的类 4. 配置Hibernate的配置文件 5. 创建Hibernate的会话工厂 6. 使用Hibernate进行数据库操作 以下是一个简单的示例,演示如何使用Hibernate: 1. 下载Hibernate并将其解压缩到你的项目目录中。 2. 在你的项目中创建一个lib文件夹,并将Hibernate的jar文件复制到该文件夹中。 3. 在你的代码中导入所需的类,例如: import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; 4. 创建Hibernate的配置文件,例如: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> </session-factory> </hibernate-configuration> 5. 创建Hibernate的会话工厂,例如: Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); 6. 使用Hibernate进行数据库操作,例如: Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); //执行数据库操作 transaction.commit(); session.close(); 以上是一个简单的示例,演示如何使用Hibernate。你可以根据你的项目需求进行修改和扩展。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值