java - 我收到此错误 org.springframework.orm.jpa.EntityManagerHolder 无法转换为 org.springframework.orm.hibernate5.SessionHolder

标签 java spring hibernate spring-boot jpa

我正在尝试将 hibernate 与 spring boot 一起使用。但我收到此错误: org.springframework.orm.jpa.EntityManagerHolder 无法转换为 org.springframework.orm.hibernate5.SessionHolder。 我是一个尝试学习 Spring Boot 的初学者。任何帮助将不胜感激。

应用程序属性:

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)

关于java - 我收到此错误 org.springframework.orm.jpa.EntityManagerHolder 无法转换为 org.springframework.orm.hibernate5.SessionHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59485720/

相关文章:

java - 来自 YearMonth 的 LocalDate 和第 n 个 DayOfWeek

java - ANTLR4 token 图像串联与混合中的注释

java - 是否可以将 PostgreSQL 中的字符字段转换为 JPA 命名查询中的整数?

java - JPA+Hibernate 强制 JPA 在延迟加载时不使用代理

java - GlassFish Server 部署和 MySQLNonTransientConnectionException

java - 如何将基于 Spring 的 RESTful 服务作为 Servlet 添加到 Tomcat

java - Spring 集成 : no output-channel or replychannel header available

java - 是否可以在单个 Rest 服务调用中检索 Image 和 JsonObject?

java - 如何使用 hibernate 正确迭代数据库记录

mysql - Hibernate 无法创建外键