1、環境準備
spring boot版本:2.3.1.RELEASE
#
網上搜索spring boot 2.x hibernate配置,很(hěn)多(duō)都是jpa例子
從關系上來說應該是spring data jpa-》hibernate-》jpa,jpa是注解規範,jpa是基于hibernate幫我們實現一些通用(yòng)的操作(zuò)。
因為(wèi)需要把舊的項目(spring mvc + spring + hibernat 3.6.5,前後端分(fēn)離)升級spring boot 2.x,這裏隻讨論hibernate的配置,畢竟除了hibernate的部分(fēn)需要升級,其他(tā)的部分(fēn)大部分(fēn)都可(kě)以通用(yòng)。
2、sessionFactory
hibernate别的不需要多(duō)說,隻需要設置sessionFactory就可(kě)以了。
spring boot可(kě)以直接從EntityManagerFactory獲取sessionFactory.
下面是修改SimpleHibernateDao的代碼,SimpleHibernateDao是舊項目裏的封裝(zhuāng)了常用(yòng)方法的基類
@Autowired
private EntityManagerFactory entityManagerFactory;
/**
* 取得sessionFactory.
*/
public SessionFactory getSessionFactory() {
return entityManagerFactory.unwrap(SessionFactory.class);
}
/**
* 取得當前Session.
*/
protected Session getSession() {
return getSessionFactory().getCurrentSession();
}
獲取session的2種方式:
1、getCurrentSession()會自動關閉session,但是要配置current_session_context_class,并且需要使用(yòng)事務(wù)
2、openSession()這種方式需要手動關閉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和entity的公(gōng)共包
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導緻的問題
問題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
這個問題就是因為(wèi)session是用(yòng)getCurrentSession()獲取,但是方法上沒有(yǒu)開啓事務(wù)導緻。
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
- 版權所有(yǒu):奇站網絡 轉載請注明出處
- 廈門奇站網絡科(kē)技(jì )有(yǒu)限公(gōng)司,專業提供網站建設,響應式網站建設,小(xiǎo)程序開發,系統定制開發。
- 軟件開發咨詢熱線(xiàn):吳小(xiǎo)姐 13313868605