Java

Java is a set of computer software and specifications developed by Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers.

Spring Logo

Spring Framework

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Hibernate Logo

Hibernate Framework

Hibernate ORM is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.

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.



java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

Problem:

While doing transactions management with the annotations, prior to Spring 3.0 we are adding aopalliance.jar file. In latest Spring bundle this jar file not available. If we add below line applicationContext.xml we need to add the aopalliance.jar file other wise it will throws the java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

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

Solution: Add the aopalliance.jar file to classpath or pom.xml file.

Click here to Download