title: Spring Data CrudRepository on java based configuration - EntityManager - no transaction is in progress
body: <p>I've read I believe tried all of the posts on this, but no luck in finding the right answer. 
  I am using java based configuration with my spring mvc project, and wanted to try Spring CrudRepository, to get away from DAOs, and that is when the whole hell broke loose:
  started with "no transaction is in progress" on flush after persist:
  - tried adding @Transactional to the method - none of the variations found here worked
  - tried changing configuration, but since it is java based, most of the answers are xml based. no luck either.
  So finally I have to ask:</p>
  
  <p>How to configure my project to make CrudRepository persist, or how to create Spring EntityManager using java configuration.</p>
  
  <p>This is the last version of my configuration file:</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = { "ba.fit.vms" })
  @ImportResource(value = "classpath:spring-security-context.xml")
  @EnableTransactionManagement
  @EnableJpaRepositories
  public class AppConfig {
  
      @Bean
      public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
          PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
          ppc.setLocation(new ClassPathResource("/persistence.properties"));
          return ppc;
      }
  
  
      // Security Configuration
      @Bean
      public KorisnickiServis korisnickiServis(){
          return new KorisnickiServis();
      }
  
      @Bean
      public TokenBasedRememberMeServices rememberMeServices() {
          return new TokenBasedRememberMeServices("remember-me-key", korisnickiServis());
      }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new StandardPasswordEncoder();
      }
  
      // Jpa Configuration
      @Value("${dataSource.driverClassName}")
      private String driver;
      @Value("${dataSource.url}")
      private String url;
      @Value("${dataSource.username}")
      private String username;
      @Value("${dataSource.password}")
      private String password;
      @Value("${hibernate.dialect}")
      private String dialect;
      @Value("${hibernate.hbm2ddl.auto}")
      private String hbm2ddlAuto;   
  
      @Bean
      public DataSource configureDataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(driver);
          dataSource.setUrl(url);
          dataSource.setUsername(username);
          dataSource.setPassword(password);
          return dataSource;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean configureEntityManagerFactory() {
          LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
          entityManagerFactoryBean.setDataSource(configureDataSource());
          entityManagerFactoryBean.setPackagesToScan("ba.fit.vms");
          entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  
          Properties jpaProperties = new Properties();
          jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, dialect);
          jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, hbm2ddlAuto);
          //jpaProperties.put(org.hibernate.cfg.Environment.SHOW_SQL, true);
          entityManagerFactoryBean.setJpaProperties(jpaProperties);
  
          return entityManagerFactoryBean;
      }
  
  
      @Bean
      public PlatformTransactionManager transactionManager() {
          final JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(configureEntityManagerFactory().getObject());
  
          return transactionManager;
      }
  
  }
  </code></pre>
  
  <p>I've tried number of variations, but was always receiving same "no transaction is in progress" error.
  Also, just a glimpse at the repos:
  LokacijaRepository:</p>
  
  <pre><code>@Transactional
  public interface LokacijaRepository extends CrudRepository&lt;Lokacija, Long&gt; {
  
  }
  </code></pre>
  
  <p>And LokacijaRepositoryImpl:</p>
  
  <pre><code>@Repository
  public class LokacijaRepositoryImpl implements LokacijaRepository {
  
      protected static Logger logger = Logger.getLogger("repo");
      @PersistenceContext // tried this as well(type= PersistenceContextType.EXTENDED)
      private EntityManager entityManager;
  
  
      @Override
      @Transactional// tried number of variations here as well, like REQUIRED...
      public &lt;S extends Lokacija&gt; S save(S entity) {
          logger.debug("trying to save!");
          try {
              entityManager.persist(entity);
              entityManager.flush();
              return entity;
          } catch (Exception e) {
              logger.debug("error: "+ e.toString());
              return null;
          }
  
      }
  </code></pre>
  
  <p>If you need anything else to help me figure this one out, let me know.</p>