spring boot 2.x 配置hibernate

  1. 新聞資訊
  2. 技術百科
行業動态 公司新聞 案例分享 技術百科

spring boot 2.x 配置hibernate

來(Come)源:奇站網絡 浏覽量:3392 發布日期: 2020-07-05

1、環境準備

spring boot版本:2.3.1.RELEASE

#

網上搜索spring boot 2.x hibernate配置,很多都是(yes)jpa例子
從關系上來(Come)說應該是(yes)spring data jpa-》hibernate-》jpa,jpa是(yes)注解規範,jpa是(yes)基于(At)hibernate幫我(I)們(them)實現一(one)些通用(use)的(of)操作(do)。
因爲(for)需要(want)把舊的(of)項目(spring mvc + spring + hibernat 3.6.5,前後端分離)升級spring boot 2.x,這(this)裏隻讨論hibernate的(of)配置,畢竟除了(Got it)hibernate的(of)部分需要(want)升級,其他(he)的(of)部分大(big)部分都可以(by)通用(use)。

2、sessionFactory

hibernate别的(of)不(No)需要(want)多說,隻需要(want)設置sessionFactory就可以(by)了(Got it)。

spring boot可以(by)直接從EntityManagerFactory獲取sessionFactory.

下面是(yes)修改SimpleHibernateDao的(of)代碼,SimpleHibernateDao是(yes)舊項目裏的(of)封裝了(Got it)常用(use)方法的(of)基類

  1. @Autowired
  2. private EntityManagerFactory entityManagerFactory;
  3. /**
  4. * 取得sessionFactory.
  5. */
  6. public SessionFactory getSessionFactory() {
  7. return entityManagerFactory.unwrap(SessionFactory.class);
  8. }
  9. /**
  10. * 取得當前Session.
  11. */
  12. protected Session getSession() {
  13. return getSessionFactory().getCurrentSession();
  14. }

獲取session的(of)2種方式:
1、getCurrentSession()會自動關閉session,但是(yes)要(want)配置current_session_context_class,并且需要(want)使用(use)事務
2、openSession()這(this)種方式需要(want)手動關閉session

3、步驟三

前面配置完,測試報no transaction is in progress錯誤
解決方法1:代碼配置

  1. spring.jpa.database=MYSQL
  2. spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
  3. spring.jpa.show-sql=true
  4. spring.jpa.hibernate.ddl-auto=none
  5. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  6. spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
  7. spring.jpa.properties.hibernate.show-sql=true
  8. spring.jpa.properties.hibernate.cache.use_second_level_cache=false
  9. spring.jpa.properties.hibernate.cache.use_query_cache=false
  1. @Configuration
  2. public class HibernateConfig {
  3. @Autowired
  4. private Environment environment;
  5. @Autowired
  6. private DataSource dataSource;
  7. @Bean
  8. public LocalSessionFactoryBean sessionFactoryBean() {
  9. LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
  10. sessionFactoryBean.setDataSource(dataSource);
  11. sessionFactoryBean.setPackagesToScan("com.qizhan100");//dao和(and)entity的(of)公共包
  12. sessionFactoryBean.setHibernateProperties(hibernateProperties());
  13. return sessionFactoryBean;
  14. }
  15. //獲取hibernate配置
  16. private Properties hibernateProperties() {
  17. Properties properties = new Properties();
  18. properties.setProperty("hibernate.current_session_context_class", environment.getProperty("spring.jpa.properties.hibernate.current_session_context_class"));
  19. properties.setProperty("hibernate.hbm2ddl.auto", environment.getProperty("spring.jpa.hibernate.ddl-auto"));
  20. properties.setProperty("hibernate.show-sql", environment.getProperty("spring.jpa.properties.hibernate.show-sql"));
  21. properties.setProperty("hibernate.cache.use_second_level_cache", environment.getProperty("spring.jpa.properties.hibernate.cache.use_second_level_cache"));
  22. properties.setProperty("hibernate.cache.use_query_cache", environment.getProperty("spring.jpa.properties.hibernate.cache.use_query_cache"));
  23. return properties;
  24. }
  25. }

解決方法2:降低hibernate版本

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <hibernate.version>5.0.12.Final</hibernate.version>
  4. </properties>
  5. <dependency>
  6. <groupId>org.hibernate</groupId>
  7. <artifactId>hibernate-core</artifactId>
  8. <version>${hibernate.version}</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.hibernate</groupId>
  12. <artifactId>hibernate-entitymanager</artifactId>
  13. <version>${hibernate.version}</version>
  14. </dependency>
  15. `

getCurrentSession導緻的(of)問題

問題1:org.hibernate.HibernateException: No CurrentSessionContext configured!

application.properties

  1. spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
問題2:Could not obtain transaction-synchronized Session for current thread

這(this)個(indivual)問題就是(yes)因爲(for)session是(yes)用(use)getCurrentSession()獲取,但是(yes)方法上沒有開啓事務導緻。

consider declaring the factory method as static for independence from its containing instance
待查

參考資料
https://www.jianshu.com/p/57e3c9a40709
https://blog.csdn.net/qq_15329947/article/details/85232287
https://blog.csdn.net/xiaonannew/article/details/82634241
https://blog.csdn.net/wt592376761/article/details/104797009

上一(one)篇 jenkins

廈門極極網絡科技有限公司

電話:13313868605

QQ:3413772931

地(land)址:廈門集美區軟件園三期


                    掃一(one)掃加我(I)咨詢