Thursday, April 3, 2014

Exception in thread "main" org.hibernate.HibernateException: persist is not valid without active transaction



Problem:  The problem in Configuration file.

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">       
        <property name="dataSource" ref="dataSource"></property>       
        <!-- Configuring the  Hibernate properties -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>               
                <prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
            </props>       
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.ranga.Employee</value>
            </list>
        </property>       
    </bean>

------------------------------------------------------------------------------------------------------------------------------

hibernate.current_session_context_class is required only for JTA transactions.

Solution: Remove the hibernate.current_session_context_class property in sessionFactory. Now the code looks like

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">       
        <property name="dataSource" ref="dataSource"></property>       
        <!-- Configuring the  Hibernate properties -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>               
            </props>       
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.ranga.Employee</value>
            </list>
        </property>       
    </bean>

Click here to see more information.



0 comments: