java - Spring Boot/Thymeleaf/Hibernate : Sessionfactory Bean with Java Annotations

标签 java spring hibernate spring-boot thymeleaf

我使用 Thymeleaf 和 Hibernate 使用 IntelliJ 创建了一个 Spring Boot Web 应用程序。到目前为止,我可以创建所有数据库连接并且工作正常。据我所知,将 Sessionfactory 作为一个 bean 并在执行数据库操作的所有服务类中 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>

干杯迈克

解决方法:

  1. 我需要添加 bean,如下所述

  2. 我不得不补充

    org.spring框架 Spring 形式 4.3.10.发布 到我的 pom.xml

  3. 我必须在@SpringBootApplication 之后添加

    @EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class})

最佳答案

由于您使用的是 spring boot,因此您应该为数据库配置使用 XML 自由配置。要将 spring boot 与 hibernate 集成,您需要像这样创建 LocalSessionFactoryBeanDataSourceHibernateTransactionManagerPersistenceExceptionTranslationPostProcessor 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 Autowiring SessionFactory 接口(interface)的实例:

 @Autowired
 SessionFactory sessionFactory;

关于java - Spring Boot/Thymeleaf/Hibernate : Sessionfactory Bean with Java Annotations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45893879/

相关文章:

java - 更新 Spring Mongo 的服务器时间

java - Apache HttpComponents : org. apache.http.client.ClientProtocolException

Java 进程的内存无限增长,但 MemoryMXBean 报告稳定堆和非堆大小

java - Spring中如何使用泛型?

java - Hibernate:保存多个关联问题

java - 在 session bean 中使用 EntityManager 时出错

java - 如何比较 Ruta 规则中两个不同注释的特征?

Spring启动示例: Unable to start embedded container error

json - Spring @ResponseBody 返回 JSON,但缺少键

java - 维护调用之间状态的 Hibernate NamingStrategy 实现