title: DAO service layer in jpaRepository
body: <p>I've been reading Spring Boot tutorials, most of the MVC patterns are implemented based on</p>
  
  <blockquote>
    <p>1) Dao interface</p>
    
    <p>2) Dao Interface Implementation</p>
    
    <p>3) Service interface for persistence</p>
    
    <p>4) Service implementation</p>
  </blockquote>
  
  <p>I think this is the good practice and I was working it. Now I try to use <strong>JpaRepository</strong> which helps to implement easily and efficiently. The configuration of my project is</p>
  
  <p><strong>Configuration class</strong></p>
  
  <pre><code>@Configuration
  @PropertySource(value = { "classpath:application.properties" })
  @EnableJpaRepositories(value={"com.dept.dao"})
  public class ApplicationContextConfig {
  
      @Autowired
      private Environment environment;
  
      @Bean(name = "dataSource")
      public DataSource getDataSource() {
          //DataSource connections
      }
  
      private Properties getHibernateProperties() {
          //Hibernate properties
      }   
  
      @Bean
      public LocalSessionFactoryBean sessionFactory() {
          LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
          sessionFactory.setDataSource(getDataSource());
          sessionFactory.setPackagesToScan(new String[] { "com.dept.model" });
          sessionFactory.setHibernateProperties(getHibernateProperties());
          return sessionFactory;
      }
  
      @Autowired
      @Bean(name = "transactionManager")
      public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
          HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
          return transactionManager;
      }
  
       @Primary
       @Bean(name="entityManagerFactory")
       public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
           final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  
           entityManagerFactoryBean.setDataSource(getDataSource());
           // mention packae scan your classes annotated as @Entity
           entityManagerFactoryBean.setPackagesToScan(new String[] { "com.dept.model" });
           entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
  
           final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
           vendorAdapter.setShowSql(true);
           entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
           entityManagerFactoryBean.setJpaProperties(getHibernateProperties());
  
           entityManagerFactoryBean.afterPropertiesSet();
           entityManagerFactoryBean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
  
           return entityManagerFactoryBean;
       }
  
       @Primary
       @Bean(name = "transactionManager")
       public PlatformTransactionManager transactionManager() {
           final JpaTransactionManager transactionManager = new JpaTransactionManager();
           try {
               transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
               transactionManager.setDataSource(getDataSource());
               transactionManager.setJpaDialect(new HibernateJpaDialect());
               transactionManager.setJpaProperties(getHibernateProperties());
           } catch (ClassNotFoundException e) {
               e.printStackTrace();
           }
           return transactionManager;
       }
  
       @Bean
       public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
           return new PersistenceExceptionTranslationPostProcessor();
       }
  
       @Bean
       public HibernateExceptionTranslator hibernateExceptionTranslator() {
           return new HibernateExceptionTranslator();
       }
  
       @Bean
       public JpaVendorAdapter japAdapter(){
           return new HibernateJpaVendorAdapter();
       }
  }
  </code></pre>
  
  <p>Example I need to <strong><em>save</em></strong> this object in database. So I implemented methods</p>
  
  <p><strong>Doa interface</strong></p>
  
  <pre><code>public interface DepartmentDao extends JpaRepository&lt;Department, Integer&gt;{
  
  }
  </code></pre>
  
  <p>I'm stuck how to implement Dao implementation, I used like this</p>
  
  <p><strong>Dao Implementation</strong> </p>
  
  <pre><code>@Repository
  public class DepartmentDaoImpl implements DepartmentDao {
  
      @Autowired
      private SessionFactory sessionFactory;
  
      // All other overridden methods
  
      @Override
      public &lt;S extends Department&gt; S save(S entity) {        
          return (S) sessionFactory.getCurrentSession().save(entity);
      }
  }
  </code></pre>
  
  <p><strong>Service interface</strong></p>
  
  <pre><code>public interface DepartmentService {
      // Other all necessary methods
      public &lt;S extends Department&gt; S save(S entity);
  }
  </code></pre>
  
  <p><strong>Service Implementation</strong></p>
  
  <pre><code>@Service
  public class DepartmentServiceImpl implements DepartmentService{
      @Autowired
      DepartmentDao departmentDao;
  
      @Override
      @Transactional
      public &lt;S extends Department&gt; S save(S entity) {        
          return departmentDao.save(entity);
      }
  }
  </code></pre>
  
  <p><strong>Controller class</strong></p>
  
  <pre><code>@RequestMapping(value = "/save", method = RequestMethod.POST)
  public ModelAndView saveDepartment(@Valid @ModelAttribute("departmentForm") Department department) {
  
      departmentService.save(department);   //departmen already autowired
      return new ModelAndView("redirect:/department/list");
  }
  </code></pre>
  
  <p>I tried to save this object in database, but no transaction happened. There were no errors also. Since I'm new to Spring Boot, I confused myself how to use jparepository. I developed this by using online reference and spent more time. </p>