正确完整的Spring Boot 1.5.x for Hibernate 5

时间:2018-02-17 16:57:29

标签: spring hibernate spring-boot hibernate-5.x

Spring Boot版本的1.5.10.RELEASE左右。 它适用于版本Hibernate

5.0.12.Final内部

目的是避免以下错误消息:

required a bean of type 'org.hibernate.SessionFactory' that could not be found

应该应用HibernateJpaSessionFactoryBean类。它来自:

这里的情况是HibernateJpaSessionFactoryBean类是@Deprecated

根据HibernateJpaSessionFactoryBean javadoc建议的解决方法是使用EntityManagerFactory#unwrap

因此来自:

手动必须声明如下:

@Bean
public SessionFactory sessionFactory(EntityManagerFactory emf) {
    return emf.unwrap(SessionFactory.class);
}

警告必须包含在以下application.properties文件中(以上共享的帖子中未提及):

spring.jpa.properties.hibernate.current_session_context_class = 
org.springframework.orm.hibernate5.SpringSessionContext

否则出现:

org.springframework.orm.jpa.JpaSystemException:
No CurrentSessionContext configured!; nested exception is org.hibernate.HibernateException:
No CurrentSessionContext configured!

直到此处@Test Hibernate@Test课程失败,这些相同的Spring Framework课程通过Hibernate传入其他项目,因此@Test public void contextLoads() { String[] beanNames = applicationContext.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { logger.info("beanName: {}", beanName); } logger.info("PlatformTransactionManager"); if(transactionManager != null) { logger.info("Class: {}", transactionManager.getClass().getName()); } } 的所有基础设施被宣布为manully。

因此通过以下代码:

beans

Spring Boot创建的所有 - PlatformTransactionManager - Class: org.springframework.orm.jpa.JpaTransactionManager 都已打印,我已确认以下内容:

HibernateTransactionManager

我希望JpaTransactionManager代替@Test

我获取@Bean方法的唯一方法是再次声明手动其他@Bean public PlatformTransactionManager transactionManager(SessionFactory sessionFactory){ HibernateTransactionManager transactionManager = new HibernateTransactionManager(); transactionManager.setSessionFactory(sessionFactory); return transactionManager; }

Spring Boot 1.5.x

因此:

  • Hibernate 5的{​​{1}}的正确完整配置是什么?

观察:如果所有内容都通过application.properties文件完全配置,那就更好了(目的是避免手动声明 任何@Bean

摘要,将Spring Boot集成到 plain Hibernate的独特方式(考虑通过{迁移完整项目的方案使用Spring Framework同时{1}} Spring Boot {} 通过以下内容:

Hibernate

加上这两个强制性要求spring.jpa.hibernate.ddl-auto = none spring.jpa.properties.hibernate.cache.provider_class = org.hibernate.cache.NoCacheProvider spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate5.SpringSessionContext spring.jpa.properties.hibernate.default_batch_fetch_size = 30 spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect spring.jpa.properties.hibernate.format_sql = true spring.jpa.properties.hibernate.jdbc.batch_size = 30 spring.jpa.properties.hibernate.max_fetch_depth = 30 spring.jpa.properties.hibernate.order_updates = true; spring.jpa.properties.hibernate.show_sql = false spring.jpa.properties.hibernate.use_sql_comments = true

@Beans

没有这两个@Bean public SessionFactory sessionFactory(EntityManagerFactory emf) { return emf.unwrap(SessionFactory.class); } @Bean public PlatformTransactionManager transactionManager(SessionFactory sessionFactory){ HibernateTransactionManager transactionManager = new HibernateTransactionManager(); transactionManager.setSessionFactory(sessionFactory); return transactionManager; } 我已经报告了所有两个错误。

因此目标是通过@Beans配置Hibernate

1 个答案:

答案 0 :(得分:1)

这是一个建议(Spring Boot 1.5.10.RELEASE)

文件 application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/vy
spring.datasource.username=root
spring.datasource.password=123456
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

文件 BeanConfig.java

package com.example;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManagerFactory;

@Configuration
public class BeanConfig {

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Bean
    public SessionFactory getSessionFactory() {
        if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
            throw new NullPointerException("factory is not a hibernate factory");
        }
        return entityManagerFactory.unwrap(SessionFactory.class);
    }

}

档案 UserDao.java

package com.example.dao;

import com.example.model.UserDetails;

import java.util.List;

public interface UserDao {

    List<UserDetails> getUserDetails();

}

文件 UserDaoImpl.java ,其中使用SessionFactory

package com.example.dao.impl;

import com.example.dao.UserDao;
import com.example.model.UserDetails;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    public List<UserDetails> getUserDetails() {
        Criteria criteria = sessionFactory.openSession().createCriteria(UserDetails.class);
        return criteria.list();
    }

}
  

目标是通过application.properties

配置Hibernate

- &GT;使用Hibernate Session实现JPA是可以的。 Spring Data JPA与Spring Boot紧密集成(至少我使用SpringBoot 2.0.0.RC1进行了检查)。通过引擎application.properties进行配置,Spring Boot是自动配置。

使用Hibernate ORM的完整功能是不行的。