EnableAutoConfiguration

EnableAutoConfiguration

我正在尝试在Spring Boot中使用休眠模式。但我收到此错误:
org.springframework.orm.jpa.EntityManagerHolder无法转换为org.springframework.orm.hibernate5.SessionHolder。
我是一个初学者,尝试学习Spring Boot。任何帮助将非常感激。

application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/userdetails
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
hibernate.dialect = org.hibernate.dialect.MySQLDialect


这是我的配置文件:

@Configuration
@PropertySource(value = "classpath:application.properties")
public class EclinicReportBeanConfig {

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${hibernate.dialect}")
    private String dialect;

    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource(url, username, password);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean localSessionFactory() {
        LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
        factory.setDataSource(getDataSource());
        factory.setHibernateProperties(hibernateProperties());
        factory.setPackagesToScan(new String[] { "com.eclinic.report" });
        return factory;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", dialect);
        properties.put("hibernate.hbm2ddl.auto", "none");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");
        return properties;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory factory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(factory);
        return transactionManager;
    }


这是我的dao文件:

@Repository
public class DHAReportDaoImpl implements DHAReportDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<UserSetup> getAllUserDetails() {

        String hql = "select userId, userName from UserSetup";
        Query<UserSetup> query = getSession().createQuery(hql);
        List<UserSetup> userlist = query.getResultList();

        return userlist;
    }

    private Session getSession() {
        Session session = sessionFactory.getCurrentSession();
        if (session == null) {
            session = sessionFactory.openSession();
        }
        return session;
    }

}


我的pom.xml依赖项:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

最佳答案

在这种情况下,问题出在@EnableAutoConfiguration-使用@EnableAutoConfiguration(exclude=HibernateJpaAutoConfiguration.class)

07-24 09:26