Mysql 8.0的Hibernate配置

时间:2019-01-01 20:44:28

标签: java mysql spring hibernate junit4

我是Java新手!当我运行Junit测试时,控制台会向我显示异常,并且我不知道该如何解决。我的Hibernate配置有什么问题?该代码完美适用于H2数据库,但是当我将数据库和配置参数更改为Mysql 8.0时,Eclipse返回异常。我希望有人能给我有用的建议,并解释如何解决此问题。谢谢。

这是我的Mysql 8.0的Hibernate配置

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan(basePackages= {"com.savaleks.websiteback.dto"})
@EnableTransactionManagement
public class HibernateConfig {

    private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/website";
    private final static String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
    private final static String DATABASE_DIALECT = "org.hibernate.dialect.MySQL8Dialect";
    private final static String DATABASE_USERNAME = "root";
    private final static String DATABASE_PASSWORD = "";

    @Bean
    public DataSource getDataSource() {

        BasicDataSource dataSource = new BasicDataSource();

        // Database connection information
        dataSource.setDriverClassName(DATABASE_DRIVER);
        dataSource.setUrl(DATABASE_URL);
        dataSource.setUsername(DATABASE_USERNAME);
        dataSource.setPassword(DATABASE_PASSWORD);

        return dataSource;
    }

    @Bean
    public SessionFactory getSessionFactory(DataSource dataSource) {

        LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);

        builder.addProperties(getHibernateProperties());
        builder.scanPackages("com.savaleks.websiteback.dto");

        return builder.buildSessionFactory();
    }

    // All Hibernate properties returned in this method
    private Properties getHibernateProperties() {

        Properties properties = new Properties();
        properties.put("hibernate.dialect", DATABASE_DIALECT);
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");

        return null;
    }

    @Bean
    public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {

        HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
        return transactionManager;

    }
}

这是控制台输出

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryDAO': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in com.savaleks.websiteback.config.HibernateConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'getSessionFactory' threw exception; nested exception is java.lang.NullPointerException

pom.xml文件

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.20.RELEASE</spring.version>
        <mysql.version>8.0.13</mysql.version>
        <hibernate.version>5.2.17.Final</hibernate.version>
    </properties>

    <dependencies>

        <!-- JUNIT TEST -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- SPRING CONTEXT -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- MYSQL DATABASE -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!-- HIBERNATE -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- DATABASE CONNECTION POOLING -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.5.0</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

如果需要更多信息,我可以加载更多的类和控制台输出。有什么事吗有什么想法吗?

1 个答案:

答案 0 :(得分:2)

更改为:

@Bean
public HibernateTransactionManager getTransactionManager() {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager(getSessionFactory());
    return transactionManager;
}

@Bean
public HibernateTransactionManager getTransactionManager(@Qualifier("getSessionFactory") SessionFactory sessionFactory) {
    // your code here
}

@Bean
public SessionFactory sessionFactory(DataSource dataSource) {
    // your code here
}

还有其他几种方法。

进一步阅读: