Spring Boot / Thymeleaf / Hibernate:具有Java注释的Sessionfactory Bean [英] Spring Boot / Thymeleaf / Hibernate: Sessionfactory Bean with Java Annotations

查看:215
本文介绍了Spring Boot / Thymeleaf / Hibernate:具有Java注释的Sessionfactory Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Thymeleaf和Hibernate使用IntelliJ创建了一个Spring Boot Web应用程序。我到目前为止,我可以创建所有的数据库连接,它工作正常。据我所见,将Sessionfactory作为一个bean并将其自动装入所有执行数据库操作的服务类中是一种很好的方法。



我有SpringMvcConfiguration的配置文件如下所示:

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扩展WebMvcConfigurerAdapter {
@Bean $ b $ public LocaleResolver localeResolver(){
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver );
sessionLocaleResolver.setDefaultLocale(Locale.GERMAN);
return sessionLocaleResolver;

$ b $Bean
LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName(lang);
返回localeChangeInterceptor;

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


$ / code $ / pre

问题:我尝试了很多,但我可以找不到为SessionFactory声明bean的解决方案。



任何提示都会非常有用。我应该在这里声明一个sessionfactory和datasource,还是必须在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>用户名< / property>
< property name =connection.password>秘密< / property>

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

<! - 如果需要,数据库模式将被更新 - >
<! - < property name =hbm2ddl.auto>更新< / property> - >
< / session-factory>
< / hibernate-configuration>

Cheers Maik



解决方案: p>


  1. 我需要添加下面提到的bean 我必须添加


    org.springframework
    spring-orm
    4.3.10.RELEASE

    到我的pom.xml

  2. 在@SpringBootApplication后,我必须添加

    @EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class})



解决方案

应该为您的数据库配置使用XML免费配置。为了将spring引导与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);

返回dataSource;


$Be
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){

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

返回txManager;


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


属性hibernateProperties(){
属性properties = new Properties();
properties.setProperty(hibernate.ddl-auto,update);
properties.setProperty(hibernate.dialect,org.hibernate.dialect.PostgreSQLDialect);
返回属性;






$ b在上面的数据库配置中我使用了postgreSQL数据库。



要获得sessionFactory自动装载 SessionFactory 接口的实例,请执行以下操作:

  @Autowired 
SessionFactory sessionFactory;


I've created a Spring Boot web application with IntelliJ using Thymeleaf and Hibernate. I've come so far that I can create all the db connections and it's working fine. As far as I've seen it would be a good way to have the Sessionfactory as a bean and autowire it in all Service Classes which do the db actions.

I have a SpringMvcConfiguration as configuration file which looks like this:

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());
    }
}

Question: I've tried a lot but I could not find a solution to declare the bean for the SessionFactory.

Any hints would be really helpful. Should I declare a sessionfactory and datasource here or does it have to be in application.properties or only in hibernate.cfg.xml which looks currently like this:

<?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>

Cheers Maik

Solution:

  1. I needed to add the beans as mentioned below

  2. I had to add

    org.springframework spring-orm 4.3.10.RELEASE to my pom.xml

  3. After @SpringBootApplication I had to add

    @EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class})

解决方案

Since you are using spring boot you should use XML free configuration for your database configuration . To integrate spring boot with hibernate you will need to create LocalSessionFactoryBean , DataSource ,HibernateTransactionManager and PersistenceExceptionTranslationPostProcessor bean like this:

@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;
    }
}

In the above database config i have used postgreSQL database .

To get instance of sessionFactory autowire SessionFactory interface like this:

 @Autowired
 SessionFactory sessionFactory;

这篇关于Spring Boot / Thymeleaf / Hibernate:具有Java注释的Sessionfactory Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆