Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Exception : No Session found for current thread

I have an application which I am developing Spring with Hibernate. Every thing went fine but when I try to test my transactions with the hibernate classes, it throwing an error saying "org.hibernate.HibernateException: No Session found for current thread". I can not find the error with me. Help would be really appreciated.

My main class :

 public static void main(String[] args) {

        Patient sac = new Patient();
        sac.setPatient_Id("M2314");
        sac.setPatient_Age(23);
        sac.setPatient_Name("Sac");
        sac.setPatient_Address("SGTY NY");


        System.out.println("load context");
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml");

        PatientService patientService = (PatientService) context.getBean("patientService");
        patientService.persistPatient(sac);

        context.close();
    }

Part of serviceImpl :

@Service("patientService")
public class PatientServiceImpl implements PatientService{

    @Autowired
    PatientDAO patientdao;

    @Override
    @Transactional
    public void persistPatient(Patient patient) {
        patientdao.persistPatient(patient);

    }

Part of DAO class :

@Repository("patientdao")
public class PatientDAOImpl implements PatientDAO{

    @Autowired
    private SessionFactory sessionFactory;


    @Override
    @Transactional
    public void persistPatient(Patient patient) {
        sessionFactory.getCurrentSession().persist(patient);

    }

My servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />


    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.tela.pms" />

   <beans:bean id="patientService" class="com.tela.pms.service.impl.PatientServiceImpl"/>
   <beans:bean id="patientdao" class="com.tela.pms.dao.impl.PatientDAOImpl"/>

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url" value="jdbc:mysql://localhost:3306/test" />
        <beans:property name="username" value="root" />
        <beans:property name="password" value="root" />
    </beans:bean>

    <beans:bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"></beans:property>
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.tela.pms.domain.Patient</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <beans:bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory">
    </beans:bean>


</beans:beans>
like image 676
Cyclops Avatar asked Dec 06 '25 23:12

Cyclops


1 Answers

In this case, I am not sure if you have enabled annotation-driven transaction management, as you have not provided the code for SpringBean.xml file.

You need to add this tag <tx:annotation-driven/> tag in your servlet-context.xml

In there also make sure to make it point to your transaction manager as follows

<tx:annotation-driven transaction-manager="transactionManager"/>

Rest of your code looks fine and this error should go away with above changes.

like image 199
AbHi Avatar answered Dec 09 '25 13:12

AbHi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!