2017-08-26 85 views
2

我使用Thymeleaf和Hibernate創建了一個使用IntelliJ的Spring Boot Web應用程序。我到目前爲止,我可以創建所有的數據庫連接,它工作正常。據我所見,將Sessionfactory作爲一個bean並將其自動裝入所有執行數據庫操作的服務類中是一種好方法。Spring Boot/Thymeleaf/Hibernate:使用Java註釋的Sessionfactory Bean

我有一個SpringMvcConfiguration作爲配置文件,它看起來像這樣:

package eu.barz.familykurse.config; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.LocaleResolver; 
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; 
import org.springframework.web.servlet.i18n.SessionLocaleResolver; 
import java.util.Locale; 


@Configuration 
public class SpringMvcConfiguration extends WebMvcConfigurerAdapter{ 
    @Bean 
    public LocaleResolver localeResolver(){ 
     SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver(); 
     sessionLocaleResolver.setDefaultLocale(Locale.GERMAN); 
     return sessionLocaleResolver; 
    } 

    @Bean 
    LocaleChangeInterceptor localeChangeInterceptor(){ 
     LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); 
     localeChangeInterceptor.setParamName("lang"); 
     return localeChangeInterceptor; 
    } 

    @Override 
    public void addInterceptors(InterceptorRegistry interceptorRegistry){ 
     interceptorRegistry.addInterceptor(localeChangeInterceptor()); 
    } 
} 

問:我已經嘗試了很多,但我無法找到一個解決方案,申報爲SessionFactory豆。

任何提示將非常有幫助。我應該在這裏宣佈一個SessionFactory和數據源或是否必須在application.properties或僅在hibernate.cfg.xml看起來目前是這樣的:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <property name="connection.url">jdbc:mysql://localhost:3306/family_kurse</property> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.username">username</property> 
     <property name="connection.password">secret</property> 

     <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 
     <property name="hibernate.current_session_context_class">thread</property> 
     <property name="show_sql">true</property> 
     <mapping class="eu.barz.familykurse.domain.Leader"/> 
     <mapping class="eu.barz.familykurse.domain.Course"/> 

     <!-- DB schema will be updated if needed --> 
     <!-- <property name="hbm2ddl.auto">update</property> --> 
    </session-factory> 
</hibernate-configuration> 

乾杯MAIK

解決方案:

  1. 我需要添加豆類下面提到

  2. 我不得不添加

    org.springframework 彈簧ORM 4.3.10.RELEASE 到我的pom.xml

  3. @SpringBootApplication後,我不得不添加

    @EnableAutoConfiguration(排除= {} HibernateJpaAutoConfiguration.class)

回答

3

由於您使用的是spring引導,因此您應該對數據庫配置使用XML免費配置。春天開機使用Hibernate集成,您需要創建LocalSessionFactoryBeanDataSourceHibernateTransactionManagerPersistenceExceptionTranslationPostProcessor豆這樣的:

@Configuration 
public class DatabaseConfig { 

    @Bean 
    public LocalSessionFactoryBean sessionFactory() { 
     LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 
     sessionFactory.setDataSource(dataSource()); 
     sessionFactory.setPackagesToScan("com.example.model"); 
     sessionFactory.setHibernateProperties(hibernateProperties()); 

     return sessionFactory; 
    } 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName("org.postgresql.Driver"); 
     dataSource.setUrl("jdbc:postgresql://localhost:5432/testdb"); 
     dataSource.setUsername("root"); 
     dataSource.setPassword("root"); 

     return dataSource; 
    } 

    @Bean 
    @Autowired 
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) { 

     HibernateTransactionManager txManager = new HibernateTransactionManager(); 
     txManager.setSessionFactory(sessionFactory); 

     return txManager; 
    } 

    @Bean 
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { 
     return new PersistenceExceptionTranslationPostProcessor(); 
    } 

    Properties hibernateProperties() { 
     Properties properties = new Properties(); 
     properties.setProperty("hibernate.ddl-auto", "update"); 
     properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); 
     return properties; 
    } 
} 

在上面的數據庫配置我已經使用PostgreSQL數據庫。

爲了得到SessionFactory的實例自動裝配SessionFactory界面是這樣的:

@Autowired 
SessionFactory sessionFactory; 
+0

你能告訴我你的依賴關係或其他模塊導入了這個例子嗎?在我的情況下,它不能解決LocalSessionFactoryBean,DriverManagerDataSource,HibernateTransactionManager和PersistenceExceptionTranslationPostProcessor ... – Barzille

+0

而且:我需要把它放在一個額外的類?你已經把它放在DatabaseConfig下,我目前只有公共類SpringMvcConfiguration擴展了WebMvcConfigurerAdapter。 – Barzille

+0

後,我加入\t \t \t \t \t org.springframework \t \t \t 彈簧ORM \t \t \t 4.3.10.RELEASE \t \t我的pom.xml模塊解決罰款。但添加autowire後,我得到以下錯誤:org.springframework.orm.jpa.EntityManagerHolder無法轉換爲org.springframework.orm.hibernate5.SessionHolder。你有什麼想法可以幫助嗎? – Barzille

0

我從here中拿過這個例子。

@Bean 
    public LocalSessionFactoryBean sessionFactory() { 
     LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 
     sessionFactory.setDataSource(restDataSource()); 
     sessionFactory.setPackagesToScan(
     new String[] { "org.baeldung.spring.persistence.model" }); 
     sessionFactory.setHibernateProperties(hibernateProperties()); 

     return sessionFactory; 
    } 

    @Bean 
    public DataSource restDataSource() { 
     BasicDataSource dataSource = new BasicDataSource(); 
     dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); 
     dataSource.setUrl(env.getProperty("jdbc.url")); 
     dataSource.setUsername(env.getProperty("jdbc.user")); 
     dataSource.setPassword(env.getProperty("jdbc.pass")); 

     return dataSource; 
    } 
+0

在這個例子中它不能解析的BasicDataSource ....那你才能使用它導入? – Barzille

+0

您是否閱讀過提供的鏈接?我不這麼認爲。別指望別人幫助每一步。 –

0

你試過嗎?

@Bean 
public org.springframework.orm.hibernate5.LocalSessionFactoryBean sessionFactory(){ 
    org.springframework.orm.hibernate5.LocalSessionFactoryBean sessionFactory = new org.springframework.orm.hibernate5.LocalSessionFactoryBean(); 
    sessionFactory.setDataSource(dataSource()); 
    return sessionFactory; 
} 
@Bean 
public DataSource dataSource() { 

    BasicDataSource dataSource = new BasicDataSource(); 
    dataSource.setDriverClassName("jdbc.driverClassName"); 
    dataSource.setUrl("jdbc.url"); 
    dataSource.setUsername("jdbc.user"); 
    dataSource.setPassword("jdbc.pass"); 

    return dataSource; 
} 
+0

在這個例子中,它無法解析org.springframework.orm.hibernate5中的BasicDataSource和orm ....你爲了使用它而導入了什麼? – Barzille

+0

我設法通過將spring-orm作爲工件添加到pom.xml來解決蠕蟲包。但BasicDataSource仍然不可用。 – Barzille

相關問題