Я столкнулся с проблемой и не смог выяснить, в чем причина. Мой класс Test, который содержит метод save to the database, фактически ничего не сохраняет в БД с аннотацией @Transactional. Но когда я удаляю @Transactional, он начинает работать. Я почти уверен, что проблема связана с менеджером транзакций, но что здесь не так?
ЭТО НЕ РАБОТАЕТ
TestSaveTransactional.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
classes = { HibernateConfiguration.class},
loader = AnnotationConfigContextLoader.class)
public class TestSaveTransactional {
@Autowired
private UserRepository userRepository;
@Test
@Transactional
public void testSaveUsersTransactional(){
System.out.println("**********testSaveUserTransactional**********");
StopWatch watch = new StopWatch();
watch.start();
final List<User> users = new LinkedList<User>();
users.add(new User("He he", true));
users.add(new User("She she", false));
this.userRepository.save(users);
watch.stop();
System.out.println(watch.getTotalTimeMillis() + " mls");
System.out.println("**********testSaveUserTransactional**********");
}
}
pom.xml
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.egs</groupId>
<artifactId>test-hibernate</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<springframework.version>4.3.9.RELEASE</springframework.version>
<hibernate.version>4.3.6.Final</hibernate.version>
<pgsql.connector.version>42.2.5</pgsql.connector.version>
<joda-time.version>2.3</joda-time.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${pgsql.connector.version}</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
HibernateConfiguration.java
@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:application.properties" })
@ComponentScan(basePackageClasses = { HibernateConfiguration.class })
@EnableJpaRepositories(basePackages = {"com.egs.repository"})
public class HibernateConfiguration {
@Value("${jdbc.driverClassName}") private String driverClassName;
@Value("${jdbc.url}") private String jdbcUrl;
@Value("${jdbc.username}") private String username;
@Value("${jdbc.password}") private String password;
@Value("${hibernate.dialect}") private String hibernateDialect;
@Value("${hibernate.show_sql}") private String showSql;
@Value("${hibernate.format_sql}") private String formatSql;
@Bean
public HibernateJpaVendorAdapter jpaVendorAdapter(){
return new HibernateJpaVendorAdapter();
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(jdbcUrl);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean sessionFactory = new LocalContainerEntityManagerFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setJpaVendorAdapter(jpaVendorAdapter());
sessionFactory.setPackagesToScan(
new String[] { "com.egs.entity" });
sessionFactory.setJpaProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", hibernateDialect);
properties.put("hibernate.show_sql", showSql);
properties.put("hibernate.format_sql", formatSql);
return properties;
}
}
ЭТО ОДИН РАБОТАЕТ
TestFindAllHibernate.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
classes = { HibernateConfiguration.class},
loader = AnnotationConfigContextLoader.class)
public class TestFindAllHibernate {
@Autowired private UserRepository userRepository;
@Autowired private PostRepository postRepository;
@Test
@Transactional(readOnly = true)
public void testFindAllUsers(){
System.out.println("**********testFindAllUsers**********");
StopWatch watch = new StopWatch();
watch.start();
System.out.println(this.userRepository.findAll());
watch.stop();
System.out.println(watch.getTotalTimeMillis() + " mls");
System.out.println("**********testFindAllUsers**********");
}
@Test
@Transactional(readOnly = true)
public void testFindAllMaleUsers(){
System.out.println("**********testFindAllMaleUsers**********");
StopWatch watch = new StopWatch();
watch.start();
System.out.println(this.userRepository.findByMaleIsTrue());
watch.stop();
System.out.println(watch.getTotalTimeMillis() + " mls");
System.out.println("**********testFindAllMaleUsers**********");
}
@Test
@Transactional(readOnly = true)
public void testFindAllPosts(){
System.out.println("**********testFindAllPosts**********");
StopWatch watch = new StopWatch();
watch.start();
System.out.println(this.postRepository.findAll());
watch.stop();
System.out.println(watch.getTotalTimeMillis() + " mls");
System.out.println("**********testFindAllPosts**********");
}
}