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)基類
@Autowired
private EntityManagerFactory entityManagerFactory;
/**
* 取得sessionFactory.
*/
public SessionFactory getSessionFactory() {
return entityManagerFactory.unwrap(SessionFactory.class);
}
/**
* 取得當前Session.
*/
protected Session getSession() {
return getSessionFactory().getCurrentSession();
}
獲取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:代碼配置
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.properties.hibernate.show-sql=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=false
spring.jpa.properties.hibernate.cache.use_query_cache=false
@Configuration
public class HibernateConfig {
@Autowired
private Environment environment;
@Autowired
private DataSource dataSource;
@Bean
public LocalSessionFactoryBean sessionFactoryBean() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setPackagesToScan("com.qizhan100");//dao和(and)entity的(of)公共包
sessionFactoryBean.setHibernateProperties(hibernateProperties());
return sessionFactoryBean;
}
//獲取hibernate配置
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.current_session_context_class", environment.getProperty("spring.jpa.properties.hibernate.current_session_context_class"));
properties.setProperty("hibernate.hbm2ddl.auto", environment.getProperty("spring.jpa.hibernate.ddl-auto"));
properties.setProperty("hibernate.show-sql", environment.getProperty("spring.jpa.properties.hibernate.show-sql"));
properties.setProperty("hibernate.cache.use_second_level_cache", environment.getProperty("spring.jpa.properties.hibernate.cache.use_second_level_cache"));
properties.setProperty("hibernate.cache.use_query_cache", environment.getProperty("spring.jpa.properties.hibernate.cache.use_query_cache"));
return properties;
}
}
解決方法2:降低hibernate版本
<properties>
<java.version>1.8</java.version>
<hibernate.version>5.0.12.Final</hibernate.version>
</properties>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
`
getCurrentSession導緻的(of)問題
問題1:org.hibernate.HibernateException: No CurrentSessionContext configured!
application.properties
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
- 版權所有:奇站網絡 轉載請注明出(out)處
- 廈門極極網絡科技有限公司,專業提供網站建設,響應式網站建設,小程序開發,系統定制開發。
- 軟件開發咨詢熱線:吳小姐 13313868605