how do i resolve getCurrentSession() in mycode

248 views Asked by At

i am beginner in java,spring&hibernate. when i using sessionFactory.getCurrentSession().saveOrUpdate(employee); igot error. but when i use sessionFactory.openSession().saveOrUpdate(employee); database insertion is possible. i want to use getCurrentSession() instead of open session .

mycode

myservice is

package com.simplecrud.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.simplecrud.dao.EmployeeDao;
import com.simplecrud.model.Employee;


@Service("employeeService")
@Transactional
public class EmployeeService {

    @Autowired
    EmployeeDao dao;

    @Transactional
    public String create(Employee employee)
    {  
        System.out.println("service"+employee);
        dao.create(employee);
        return null;
    }

}

my dao is

package com.simplecrud.dao;


import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;


import com.simplecrud.model.Employee;


@Component
@Repository
public class EmployeeDao {

 @Autowired
 SessionFactory sessionFactory;

 public void create(Employee employee){

     sessionFactory.openSession().saveOrUpdate(employee);
     /*sessionFactory.getCurrentSession().saveOrUpdate(employee);*/
    /* protected Session getSession(){
            return sessionFactory.getCurrentSession();
        }*/
 }
}

config file

package com.simplecrud.configuration;  

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.ComponentScan;  
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;  
import org.springframework.web.servlet.view.JstlView;  
import org.springframework.web.servlet.view.UrlBasedViewResolver;  

@Configuration //Marks this class as configuration
//Specifies which package to scan
@ComponentScan("com.simplecrud")
//Enables Spring's annotations 
@EnableWebMvc   
public class Config {  

    @Bean  
    public UrlBasedViewResolver setupViewResolver() {  
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();  
        resolver.setPrefix("/WEB-INF/views/");  
        resolver.setSuffix(".jsp");  
        resolver.setViewClass(JstlView.class);  
        return resolver;  
    } 
    @Bean(name = "dataSource")
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/employee_db");
        driverManagerDataSource.setUsername("root");
        driverManagerDataSource.setPassword("mysql");
        return driverManagerDataSource;
    }

     private Properties getHibernateProperties() {
            Properties properties = new Properties();
            properties.put("hibernate.show_sql", "true");
            properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        //    properties.put("hibernate.hbm2ddl.auto", "update");
            properties.put("hibernate.c3p0.minPoolSize", "5");
            properties.put("hibernate.c3p0.maxPoolSize", "10");
            properties.put("hibernate.c3p0.idleTestPeriod", "300");
            properties.put("hibernate.c3p0.timeout", "600");
            properties.put("hibernate.c3p0.max_statement", "50");
            properties.put("hibernate.c3p0.testConnectionOnCheckout", "false");
            properties.setProperty("timeZone", "serverTimezone");
            properties.put("hibernate.c3p0.preferredTestQuery", "select 1");
            return properties;
        }


        @Autowired
        @Bean(name = "sessionFactory")
        public SessionFactory getSessionFactory(DataSource dataSource) {
            LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
            sessionBuilder.addProperties(getHibernateProperties());
            sessionBuilder.scanPackages("com.simplecrud.model");
            //sessionBuilder.setInterceptor(new MySQLCalcFoundRowsInterceptor());
            return sessionBuilder.buildSessionFactory();
        }


        @Autowired
        @Bean(name = "transactionManager")
        public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
            HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
            return transactionManager;
        } 


}  

help me anyone..

0

There are 0 answers