Advertisement
Guest User

Untitled

a guest
Dec 24th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. @Configuration
  2. @EnableTransactionManagement
  3. @EnableWebMvc
  4. @ComponentScan(basePackages = "io.github.bibekshakya35.ehealth")
  5. public class EhealthCofiguration {
  6.  
  7. @Bean
  8. public ViewResolver getViewResolver() {
  9. InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
  10. viewResolver.setViewClass(JstlView.class);
  11. viewResolver.setPrefix("/WEB-INF/view/");
  12. viewResolver.setSuffix(".jsp");
  13. return viewResolver;
  14. }
  15.  
  16. @Bean(name = "dataSource")
  17. public javax.sql.DataSource getDataSource() {
  18. BasicDataSource dataSource = new BasicDataSource();
  19. dataSource.setDriverClassName("org.postgresql.Driver");
  20. dataSource.setUrl("jdbc:postgresql://localhost:5432/ehealth");
  21. dataSource.setUsername("test");
  22. dataSource.setPassword("test123");
  23. return dataSource;
  24. }
  25.  
  26. @Autowired
  27. @Bean(name = "sessionFactory")
  28. public SessionFactory getSessionFactory(javax.sql.DataSource dataSource) {
  29. LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
  30. sessionBuilder.scanPackages("io.github.bibekshakya35.ehealth.model");
  31. sessionBuilder.addProperties(getHibernateProperties());
  32. return sessionBuilder.buildSessionFactory();
  33. }
  34.  
  35. private Properties getHibernateProperties() {
  36. Properties properties = new Properties();
  37. properties.put("hibernate.show_sql", "true");
  38. properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
  39. properties.put("hibernate.hbm2ddl.auto", "update");
  40. return properties;
  41. }
  42.  
  43. @Autowired
  44. @Bean(name = "transactionManager")
  45. public HibernateTransactionManager getTransactionManager(
  46. SessionFactory sessionFactory) {
  47. HibernateTransactionManager transactionManager = new HibernateTransactionManager(
  48. sessionFactory);
  49.  
  50. return transactionManager;
  51. }
  52. }
  53.  
  54. public class EhealthWebAppIntializer implements WebApplicationInitializer {
  55.  
  56. @Override
  57. public void onStartup(ServletContext servletContext) throws ServletException {
  58. AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
  59. applicationContext.register(EhealthCofiguration.class);
  60. ServletRegistration.Dynamic dispatcher =servletContext.addServlet("SpringDispatcher", new DispatcherServlet(applicationContext));
  61. dispatcher.setLoadOnStartup(1);
  62. dispatcher.addMapping("/");
  63. }
  64.  
  65. @Repository
  66. @Transactional
  67. public class HibernateDAO<T extends Serializable> implements IGenericDao<T> {
  68.  
  69. private Class<T> clazz;
  70.  
  71. Session session;
  72.  
  73. @Autowired
  74. SessionFactory sessionFactory;
  75.  
  76. private static final Logger LOG = Logger.getLogger(HibernateDAO.class.getName());
  77.  
  78. @Override
  79. public void setClazz(Class<T> clazzToSet) {
  80. this.clazz = clazzToSet;
  81. }
  82.  
  83. @Override
  84. public void create(T entity) {
  85. session = getCurrentSession();
  86. LOG.log(Level.INFO, "inside create entity and you just bind your session to the current one{0}", session.toString());
  87. session.saveOrUpdate(entity);
  88. LOG.info("saved");
  89. session.flush();
  90. session.refresh(entity);
  91. }
  92.  
  93.  
  94.  
  95. protected Session getCurrentSession() {
  96. return sessionFactory.getCurrentSession();
  97. }
  98.  
  99.  
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement