Simultaneous use of Hibernate and Spring data jpa?

后端 未结 3 1859
孤独总比滥情好
孤独总比滥情好 2021-01-31 03:33

Is it possible to use Spring Data JPA (backed by Hibernate as JPA provider) and directly use Hibernate at the same time?

The problem is that when i use JpaTransactionMan

相关标签:
3条回答
  • 2021-01-31 04:01

    This is what I did, and it worked well: one data source, two transaction manager.
    Data source bean:

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- bulabula... -->
    </bean>
    

    For Hibernate XML based configuration:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingLocations" value="#{propertyUtils.getList('hibernate.hbm')}"/>
        <property name="hibernateProperties">
            <value>
                <!-- bulabulabula... -->
            </value>
        </property>
    </bean>
    
    <bean id="transactionManager" primary="true" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    

    And for spring-data-jpa java based configuration:

    @Configuration
    @EnableJpaRepositories(basePackages = {"org.sharder.core.repository"}, 
    transactionManagerRef = "jpaTransactionManager")
    @EnableTransactionManagement
    public class JpaConfig {
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(ComboPooledDataSource comboPooledDataSource) {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("org.sharder.core.entity");
        factory.setDataSource(comboPooledDataSource);
        factory.setJpaProperties(getHibernateProperties());
        return factory;
    }
    
    @Bean(name = "jpaTransactionManager")
    public PlatformTransactionManager jpaTransactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory);
        return txManager;
    }
    
    private Properties getHibernateProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
        properties.setProperty("hibernate.cache.use_query_cache", "true");
        properties.setProperty("hibernate.cache.use_second_level_cache", "true");
        properties.setProperty("hibernate.cache.use_structured_entries", "true");
        properties.setProperty("hibernate.format_sql", "true");
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.use_sql_comments", "true");
        properties.setProperty("hibernate.query.substitutions", "true 1, false 0");
        properties.setProperty("hibernate.jdbc.fetch_size", "20");
        properties.setProperty("hibernate.connection.autocommit", "false");
        properties.setProperty("hibernate.connection.release_mode", "auto");
        return properties;
    }
    

    }

    Notice that, transactionManagerRef = "jpaTransactionManager" set the JpaTransactionManager to be used with the repositories. Spring Data JPA namespace attributes

    0 讨论(0)
  • 2021-01-31 04:03

    Instead of creating a SessionFactory, use EntityManager.unwrap(Session.class) to get a Hibernate Session and retrieve the session factory from the Session object.

    You can also use EntityManagerFactory.unwrap(SessionFactory.class) to get the Hibernate SessionFactory directly.

    0 讨论(0)
  • 2021-01-31 04:07

    You need a single way of configuration you are now configuring both Hibernate and JPA. You should be using JPA for configuration so remove the hibernate setup.

    You are using Hibernate4 so you can take advantage of the, not so well known, HibernateJpaSessionFactoryBean of Spring. If you need access to the SessionFactory (which I assume you need).

    When applied your configuration will like something like this.

    <bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="entityManagerFactory"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
    </bean>
    

    I would suggest to only use this as an intermediate solution while you are refactoring your applicaiton to use the plain JPA api. I wouldn't suggest mixing both strategies.

    0 讨论(0)
提交回复
热议问题