Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- @EnableTransactionManagement
- @EnableWebMvc
- @ComponentScan(basePackages = "io.github.bibekshakya35.ehealth")
- public class EhealthCofiguration {
- @Bean
- public ViewResolver getViewResolver() {
- InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
- viewResolver.setViewClass(JstlView.class);
- viewResolver.setPrefix("/WEB-INF/view/");
- viewResolver.setSuffix(".jsp");
- return viewResolver;
- }
- @Bean(name = "dataSource")
- public javax.sql.DataSource getDataSource() {
- BasicDataSource dataSource = new BasicDataSource();
- dataSource.setDriverClassName("org.postgresql.Driver");
- dataSource.setUrl("jdbc:postgresql://localhost:5432/ehealth");
- dataSource.setUsername("test");
- dataSource.setPassword("test123");
- return dataSource;
- }
- @Autowired
- @Bean(name = "sessionFactory")
- public SessionFactory getSessionFactory(javax.sql.DataSource dataSource) {
- LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
- sessionBuilder.scanPackages("io.github.bibekshakya35.ehealth.model");
- sessionBuilder.addProperties(getHibernateProperties());
- return sessionBuilder.buildSessionFactory();
- }
- private Properties getHibernateProperties() {
- Properties properties = new Properties();
- properties.put("hibernate.show_sql", "true");
- properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
- properties.put("hibernate.hbm2ddl.auto", "update");
- return properties;
- }
- @Autowired
- @Bean(name = "transactionManager")
- public HibernateTransactionManager getTransactionManager(
- SessionFactory sessionFactory) {
- HibernateTransactionManager transactionManager = new HibernateTransactionManager(
- sessionFactory);
- return transactionManager;
- }
- }
- public class EhealthWebAppIntializer implements WebApplicationInitializer {
- @Override
- public void onStartup(ServletContext servletContext) throws ServletException {
- AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
- applicationContext.register(EhealthCofiguration.class);
- ServletRegistration.Dynamic dispatcher =servletContext.addServlet("SpringDispatcher", new DispatcherServlet(applicationContext));
- dispatcher.setLoadOnStartup(1);
- dispatcher.addMapping("/");
- }
- @Repository
- @Transactional
- public class HibernateDAO<T extends Serializable> implements IGenericDao<T> {
- private Class<T> clazz;
- Session session;
- @Autowired
- SessionFactory sessionFactory;
- private static final Logger LOG = Logger.getLogger(HibernateDAO.class.getName());
- @Override
- public void setClazz(Class<T> clazzToSet) {
- this.clazz = clazzToSet;
- }
- @Override
- public void create(T entity) {
- session = getCurrentSession();
- LOG.log(Level.INFO, "inside create entity and you just bind your session to the current one{0}", session.toString());
- session.saveOrUpdate(entity);
- LOG.info("saved");
- session.flush();
- session.refresh(entity);
- }
- protected Session getCurrentSession() {
- return sessionFactory.getCurrentSession();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement