Answer the question
In order to leave comments, you need to log in
How does PlatformTransactionManager transactionManager(EntityManagerFactory emf) work?
Good day everyone! I switched from xml configuration to java config, everything works fine, only misunderstanding of one thing torments me: When I create:
public PlatformTransactionManager transactionManager(EntityManagerFactory emf)
then there is a reference to the EntityManagerFactory emf in the method arguments. In xml, when I did transactionManager, it referred to the factory, here, as I understand it, the same thing happens, but apparently I have a small gap in knowledge, because I did similar things, but I also did not understand at what point the object was created, is picked up, i.e. I feel, but I do not see an obvious connection. Maybe, if in this example all the beans are singletons, when the context is raised, it matches them? import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import javax.persistence.EntityManagerFactory;
import java.util.Properties;
@Configuration
@ComponentScan("com.springapp.mvc")
@EnableWebMvc
@EnableTransactionManagement
public class MvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mydbtest?useSSL=false");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setPassword("root");
return driverManagerDataSource;
}
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean EMFactory = new LocalContainerEntityManagerFactoryBean();
EMFactory.setDataSource(dataSource());
EMFactory.setPackagesToScan("com.springapp.mvc.Entities");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
EMFactory.setJpaVendorAdapter(vendorAdapter);
EMFactory.setJpaProperties(additionalProperties());
return EMFactory;
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
return properties;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question