如何将Hibernate与Spring Boot集成。?
我在将hibernate与SpringBoot集成时遇到了一些问题 我的HibernateUtil.java类如何将Hibernate与Spring Boot集成。?,hibernate,spring-boot,Hibernate,Spring Boot,我在将hibernate与SpringBoot集成时遇到了一些问题 我的HibernateUtil.java类 @Configuration public class HibernateUtil { @Autowired private EntityManagerFactory factory; @Bean public SessionFactory getSessionFactory() { if(factory.unwrap(Session
@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.java类
@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();
}
}
}
在实现了这一点之后,我仍然会遇到这个错误
说明:
com.demo.dao.EmployeeDAO中的字段sessionFactory需要找不到“org.hibernate.sessionFactory”类型的bean
行动:
考虑在您的配置中定义“org.hibernate.SessionFactory”类型的bean。您无需手动创建会话,Spring boot将自动处理 创建一个接口
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, int> {
/* note int I am considering employee id will be integer or you can use required type in the second parameter as per primary key */
}
@存储库
公共接口EmployeeRepository扩展了JpaRepository{
/*注:我认为员工id将是整数,或者您可以根据主键在第二个参数中使用必需的类型*/
}
在服务类中自动连接此EmployeeRepository。您将能够使用数据层
您可以在此链接中阅读更多内容我知道这一点。但我的问题是我想将Hibernate与SpringBoot结合使用。