【发布时间】:2017-12-17 17:14:39
【问题描述】:
我需要将旧项目迁移到新平台。还有一些其他项目已经迁移到 Spring Boot 项目。但它是由一些现在不在我组织中的才华横溢的成员完成的。
想知道在最新的Spring-data-jpa/Hibernate上使用如下代码进行数据访问层的优缺点(我擅长boot+data)。
根据我个人的理解,每次它调用一个 dao 时都会有一个新的休眠会话,并且该会话将在该 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);
}
}
提前致谢...
【问题讨论】:
标签: hibernate jpa orm spring-data spring-data-jpa