0
votes

I have created a spring-boot based java application that uses spring's tasks. This is a console based java application that also uses hibernate to access database. This application is based on a spring sample for tasks and uses annotations instead of configuration files. For hibernate, I am actually using hibernate.cfg.xml and am initializing the hibernate session factory myself.

I wanted to use Spring's declaration transaction management but they are not working because I have not been able to find sample of how to initialize spring session factory through annotations. All samples show configuration through a configuration files. Can anybody point to how to initialize hibernate session factory through spring using annotations and code so that spring's declarative transactions would work.

Thanks in advance.

Waqar

1

1 Answers

0
votes
 @Configuration
 @EnableTransactionManagement
 public class AppConfig {
 @Bean
 public FooRepository fooRepository() {
     // configure and return a class having @Transactional methods
     return new JdbcFooRepository(dataSource());
 }

 @Bean
 public DataSource dataSource() {
     // configure and return the necessary JDBC DataSource
 }

 @Bean
 public PlatformTransactionManager txManager() {
     return new DataSourceTransactionManager(dataSource());
 }
}