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.



Related Posts:

  • New Features in Spring Framework 2.5:New Features in Spring Framework 2.5:Annotation-driven configuration:  Annotation-driven dependency injection through @Autowired annotation and fine-grained auto wiring control with@Qualifier.Support for JSR-250 annotati… Read More
  • Migrating to Spring Framework 4.0Hi friends to migrate from 3.x to 4.x the documentation is available in git hub. Click here to see.orGit Hub URL: https://github.com/spring-projects/spring-framework/wiki/Migrating-from-earlier-versions-of-the-spring-framewor… Read More
  • New Features in Spring Framework 3.xNew Features in Spring Framework 3.x:Java 5 Support - The core API of Spring 3.0 framework is using Java 5, so Java5 or above is required to run Spring 3.0 based applications. Java 5 features such as generics and annotations … Read More
  • Difference between Spring and Struts framework:The major differences between Spring framework and Struts framework are :SpringStrutsSpring is an application framework in which Spring MVC is one of the modules of Spring framework.Struts is a web framework which can be used… Read More
  • Step by Step to develop Spring4.0 ApplicationStep1:  Create Java ProjectThe first step is to create a simple Java Project using Eclipse IDE. Follow the option File -> New -> Project and finally select Java Project wizard from the wizard list. Now name your pr… Read More

0 comments: