我已经使用 Thymeleaf 和 Hibernate 使用 IntelliJ 创建了一个 Spring Boot Web 应用程序。到目前为止,我可以创建所有数据库连接,并且工作正常。就我所见,将 Sessionfactory 作为 bean 并在执行 db 操作的所有服务类中 Autowiring 它是一种好方法。
我有一个 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 声明 bean 的解决方案。
任何提示都会非常有帮助。我应该在此处声明 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>
干杯麦克
解决方案:
org.springframework
Spring
4.3.10. 发布
到我的 pom.xml
@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class})
最佳答案
由于您使用的是 spring boot,因此您应该为您的数据库配置使用 XML 自由配置。要将 Spring Boot 与 hibernate 集成,您需要像这样创建 LocalSessionFactoryBean
、 DataSource
、 HibernateTransactionManager
和 PersistenceExceptionTranslationPostProcessor
bean:
@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 autowire
SessionFactory
接口(interface)的实例: @Autowired
SessionFactory sessionFactory;
关于java - Spring Boot/Thymeleaf/Hibernate : Sessionfactory Bean with Java Annotations,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45893879/