Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何实现Spring boot+;冬眠_Java_Spring_Spring Boot_Spring Data Jpa - Fatal编程技术网

Java 如何实现Spring boot+;冬眠

Java 如何实现Spring boot+;冬眠,java,spring,spring-boot,spring-data-jpa,Java,Spring,Spring Boot,Spring Data Jpa,我得到这个错误: *************************** 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: Con

我得到这个错误:

*************************** 
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);
    }
}
@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();
        }
    }
}
我的
EmployeeDao
课程是:

@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);
    }
}
@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.dialogue=org.hibernate.dialogue.mysqldialogue


spring.jpa.properties.hibernate.current\u session\u context\u class=org.springframework.orm.hibernate4.SpringSessionContext尝试在存储库中创建
自动连线
会话工厂

   @Repository
   public class EmployeeDAO {
            private SessionFactory sessionFactory;

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

或者尝试此解决方案-

尝试在您的存储库中创建
自动连线
会话工厂

   @Repository
   public class EmployeeDAO {
            private SessionFactory sessionFactory;

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

或者尝试此解决方案-

您无需创建会话。只需在pom.xml中添加依赖项spring boot starter jpa即可使用hibernate。

org.springframework.boot
spring引导启动器数据jpa


如果这样做,则可以直接在存储库中进行查询。

无需创建会话。只需在pom.xml中添加依赖项spring boot starter jpa即可使用hibernate。

org.springframework.boot
spring引导启动器数据jpa


如果您这样做,那么您可以直接在存储库中进行查询。

请发布.property文件这是您唯一的错误吗?您似乎正在创建SessionFactorybean。但也许创建那个bean时出错了?与无法创建EntityManagerFactory类似,请在getSessionFactory()中放置一条日志语句,或放置一个断点,然后在调试模式下启动应用程序。我很确定这个方法永远不会被调用。当配置类不在Spring的默认类路径扫描机制要查看的位置时,可能会发生这种情况。请发布属性文件这是您唯一的错误吗?您似乎正在创建SessionFactorybean。但也许创建那个bean时出错了?与无法创建EntityManagerFactory类似,请在getSessionFactory()中放置一条日志语句,或放置一个断点,然后在调试模式下启动应用程序。我很确定这个方法永远不会被调用。当配置类不在Spring默认类路径扫描机制的位置时,就会发生这种情况。我只想在EmployeeDAO中处理“save”方法。并且只希望将EntityManagerFactory保留在HibernateUtil类中。因此,我可以在各种类中使用“会话工厂”。为什么此解决方案会导致循环依赖关系?它会创建entitymanager,但在自动连线的情况下会出现循环依赖错误:我只想在EmployeeDAO中处理“save”方法。我只想在HibernateUtil类中保留EntityManagerFactory。这样我就可以使用“不同类中的会话工厂。为什么此解决方案会导致循环依赖关系?它创建一个entitymanager,但自动连接会导致循环依赖项错误:S