SERVICE layer in jpaRepository

I've been reading spring-boot tutorials, most of the mvc patterns are implemented based on

1) Dao interface

2) Dao Interface Implementation

3) Service interface for persistance

4) Service implementation

I think this is the good practice and I was working it. Now I try to use JpaRepository which helps to implement easily and efficiently. The configuration of my project is

Configuration class

@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();
     }
}

Example I need to save this object in database. So I implemented methods

Doa interface

public interface DepartmentDao extends JpaRepository<Department, Integer>{

}

I'm stuck how to implement Dao implementation, I used like this

Dao Implementation

@Repository
public class DepartmentDaoImpl implements DepartmentDao {

    @Autowired
    private SessionFactory sessionFactory;

    // All other overridden methods

    @Override
    public <S extends Department> S save(S entity) {        
        return (S) sessionFactory.getCurrentSession().save(entity);
    }
}

Service interface

public interface DepartmentService {
    // Other all necessary methods
    public <S extends Department> S save(S entity);
}

Service Implementation

@Service
public class DepartmentServiceImpl implements DepartmentService{
    @Autowired
    DepartmentDao departmentDao;

    @Override
    @Transactional
    public <S extends Department> S save(S entity) {        
        return departmentDao.save(entity);
    }
}

Controller class

@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");
}

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. I tried my very best but I could not make it. Please help me to sort this out. Thanks in advance

链接地址: http://www.djcxy.com/p/87896.html

上一篇: 如何让你的数据结构能够接收任何类的对象

下一篇: JpaRepository中的SERVICE层