在SpringBoot + Spring数据中配置SessionFactory JPA + MySql项目

时间:2017-01-17 17:12:42

标签: hibernate spring-boot spring-data-jpa sessionfactory

我一直用SpringBoot + Spring Data JPA编写我的第一个项目。我使用MySql作为我的数据库提供程序。我是Spring Boot和Hibernate的新手。

我按照http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/提供的教程进行了我的项目。

在会议工厂之前,一切都很清楚。上述教程不使用会话工厂;相反,它使用基于CrudRepository的交易。但就我而言,由于我还是Hibernate的新手,我使用SessionFactory进行数据库操作。

在我的项目中,DaoImpl类看起来如下。

@Repository
public class ReadDaoImpl implements ReadDao{

    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Autowired
    public static void setSessionFactory(SessionFactory sessionFactory) {
        TenderReadDaoImpl.sessionFactory = sessionFactory;
    }

    @Override
    public List<Read> getAllReads() {

        System.out.println("DEBUG: Inside ReadDaoImpl.getAllReads");

        Session session = sessionFactory.openSession(); // I think that the issue comes from here
        Transaction tx = null;
        ..............................
        ..............................
    }
}

我的项目开始在服务器上运行,没有任何问题。但是当我访问getAll()方法的相关url时,Eclipse控制台会打印出一个空指针异常,引用以下行。

Session session = sessionFactory.openSession();

我认为我错过了sessionFactory的一些配置。但我无法弄清楚它是什么。

这是我的application.properties文件。它实际上是上述链接中同一文件的副本。

# ===============================
# = DATA SOURCE
# ===============================

# Set here configurations for the database connection

# Connection url for the database 
spring.datasource.url = jdbc:mysql://localhost:3306/readsdb?useSSL=false

# Username and password
spring.datasource.username = root
spring.datasource.password = password

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# ===============================
# = JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = create-drop

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

在我的项目中,没有任何配置文件。我是否需要添加一个来创建sessionFactory Bean?我遇到了一个叫entityManagerFactory的东西。我是否需要为此基于Spring Jpa的项目配置sessionfactory?如果是这样,怎么样?

我现在完全没有解决方案了。指导我完成这件事。如果能够指出这样做的详尽方法,那将对我有所帮助。

谢谢..!

1 个答案:

答案 0 :(得分:0)

已通过这种方式在@Configuration类中显式配置了SessionFactory。从属性文件中删除配置。我使用Oracle,可以相应地替换mysql属性:

@Configuration
public class myConfiguration {
 //datasource bean
    @Bean("dataSource")
    public DataSource  getDataSource() {        
        DataSourceBuilder b = DataSourceBuilder.create();
         b.url("jdbc:oracle:thin:@localhost:1521:xe");
         b.driverClassName("oracle.jdbc.driver.OracleDriver");
         b.username("system");
         b.password("system");
        return b.build();
    }


    //sessionfactory bean
    @Bean
    public SessionFactory getSessionFactory(DataSource dataSource) {

        LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);

        builder.addProperties(getHibernateProperties());
        builder.scanPackages("ORM.Model");

        return builder.buildSessionFactory();       
    }

    //Hibernate properties:
    private Properties getHibernateProperties() {       
        Properties properties = new Properties();

        properties.put("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect");        
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");     
        properties.put("hibernate.hbm2ddl.auto", "create");

        return properties;
    }

    //transaction manager bean
    @Bean
    public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
        return transactionManager;
    }