Spring数据jpa或客户端实现

时间:2017-07-13 05:10:40

标签: hibernate jpa orm spring-data spring-data-jpa

我需要将旧项目迁移到新平台。还有一些其他项目已经迁移到spring boot项目。但它是由一些现在不在我组织中的才华横溢的成员完成的。

我想知道在最新的Spring-data-jpa / Hibernate上使用以下代码进行数据访问的优点和缺点(我很喜欢boot + data)。

从我个人的理解中,每当它调用一个新的hibernate会话,并且该会话将在该dao完成的工作后立即提交。如果用户会话请求使用两个dao类,则在这种情况下如何进行事务管理。我看到这个代码用这个新配置覆盖了上下文。

这个问题的最终期望是遵循此代码或弹簧数据的天气?以及有哪些优势

        package com.xxxx

    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    import io.searchbox.client.JestClientFactory;
    import org.apache.commons.dbcp2.BasicDataSource;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScan.Filter;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
    import org.springframework.orm.hibernate4.HibernateTransactionManager;
    import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
    import org.springframework.stereotype.Controller;
    import org.springframework.transaction.annotation.EnableTransactionManagement;

    import javax.sql.DataSource;
    import java.util.Locale;
    import java.util.Properties;

    @Configuration
    @SuppressWarnings("PMD.UseSingleton")
    @ComponentScan(basePackageClasses = Application.class,
            excludeFilters = @Filter({Controller.class, Configuration.class}))
    @EnableTransactionManagement
    public class ApplicationContextConfig {

        @Autowired
        private ConnectorConfiguration connectorConfiguration;

        @Autowired
        private WebNotifyConnectorConfiguration webNotifyConnectorConfiguration;

        @Bean(name = "webNotifyDataSource")
        public DataSource getWebNotifyDataSource() {
            return getBasicDataSource(webNotifyConnectorConfiguration);
        }

        @Autowired
        @Bean(name = "webNotifySessionFactory")
        public SessionFactory getWebNotifySessionFactory(DataSource webNotifyDataSource) {
            LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(webNotifyDataSource);
            sessionBuilder.addProperties(getHibernateProperties());
            sessionBuilder.addAnnotatedClasses(PartnerNotify.class);
            return sessionBuilder.buildSessionFactory();
        }

        @Autowired
        @Bean(name = "webNotifyTransactionManager")
        public HibernateTransactionManager getWebNotifyTransactionManager(SessionFactory webNotifySessionFactory) {
            return new HibernateTransactionManager(webNotifySessionFactory);
        }

        private BasicDataSource getBasicDataSource(IConnectorConfiguration connectorConfiguration) {
            final BasicDataSource dataSource = new BasicDataSource();
            dataSource.setDriverClassName(connectorConfiguration.getDriverClassName());
            dataSource.setUrl(connectorConfiguration.getConnectionUrl());
            dataSource.setUsername(connectorConfiguration.getConnectionUsername());
            dataSource.setPassword(connectorConfiguration.getConnectionPassword());
            return dataSource;
        }

        private Properties getHibernateProperties() {
            Properties properties = new Properties();
            properties.put("hibernate.show_sql", "true");
            properties.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
            return properties;
        }

        @Autowired
        @Bean(name = "partnerNotifyDao")
        public PartnerNotifyDao getPartnerNotifyDao(SessionFactory webNotifySessionFactory) {
            return new PartnerNotifyDaoImpl(webNotifySessionFactory);
        }
    }

提前致谢...

1 个答案:

答案 0 :(得分:1)

你的例子没有错。

只是在Spring Boot之前我们必须手动定义所有这些bean,无论我们使用的是Java @Configuration还是旧的基于xml的配置。

  

如果用户会话请求使用两个dao类如何进行事务处理   在这种情况下正在进行管理。

嗯,这取决于如何配置事务。更具体地说,那些@Transactional annotatioins以及如何在那些上设置传播。

使用Spring Boot + Spring Data JPA的第一个好处是您不必编写这样的配置。相反,你只需@EnableAutoConfiguration,@ EnableJpaRepositories并在application.yml中放置一些配置,你就可以获得由Boot实现的DataSouce,Hibernate SessionFactory等。

ApplicationContextConfig:

@Configuration
@SuppressWarnings("PMD.UseSingleton")
@ComponentScan(basePackageClasses = Application.class,
        excludeFilters = @Filter({Controller.class, Configuration.class}))
@EnableJpaRepositories(basePackageClasses = {PartnerNotifyDao.class})
@EntityScan(basePackageClasses = User.class)
@EnableTransactionManagement
public class ApplicationContextConfig {

}

application.yml:

spring:
  datasource:
    driver-class-name: <your driver>
    url: <jdbc url>
    username: <db username>
    password: <db password>
  jpa:
    hibernate:
      ddl-auto: validate
    database: oracle
    show-sql: true

如果你的应用很简单,只使用一个数据源,一个tx管理器我建议使用Spring Boot简化配置。