Exception - createQuery is not valid without active transaction

679 views Asked by At

I need to use Transactional annotation. How it should be done? Now , exception is org.hibernate.HibernateException: createQuery is not valid without active transaction. If I remove thread, that will be exception - No Session found for current thread. What else should I do?

CoursesDAO.java

public interface CoursesDAO {

public Course createCourse(Course course);

public Course findCourseById(Integer key);

public Course updateCourse(Course course);

public void deleteCourse(Course course);

public List<Course> getAllCourses();

public List<Course> getAllCoursesByCategory(String category);

public List<Course> getAllCoursesWhichNoProposal( );

}

CoursesDAOImpl.java

@Repository
public class CoursesDAOImpl implements CoursesDAO {

 @Autowired
 private SessionFactory sessionFactory;

public Course createCourse(Course course) {

 Session session = sessionFactory.getCurrentSession();

    Integer id = (Integer) sessionFactory.getCurrentSession().save(course);
    course.setId(id);

    return course;
};

public Course findCourseById(Integer id) {

    Session session = sessionFactory.getCurrentSession();
    Course course = (Course) session.get(Course.class, id);
    return course;
}


public Course updateCourse(Course course) {

    Session session = sessionFactory.getCurrentSession();
    session.merge(course);
    return course;
};

public void deleteCourse(Course course) {

    Session session = sessionFactory.getCurrentSession();
    session.delete(course);

};

public List<Course> getAllCourses() {

    Session session = sessionFactory.getCurrentSession();
        List listCourses = session.createQuery("from Course").list();
    return listCourses;
}



public List<Course> getAllCoursesByCategory(String category) {

    Session session = sessionFactory.getCurrentSession();
        List listCoursesByCategory = session.createQuery("from Course c where c.category='"+category+"'").list();
    return listCoursesByCategory;
}


public List<Course> getAllCoursesWhichNoProposal( ) {

    Session session = sessionFactory.getCurrentSession();
        List listCoursesNoProposal = session.createQuery("from Course c where c.state not like 'Proposal' and c.state not like 'Rejected'").list();
    return listCoursesNoProposal;
}
}

CourseService.java

public interface CourseService {

public Course findCourseById(Integer id);

public Course updateCourse(Course course);

public Course createCourse(Course course);

public void deleteCourse(Course course);

public List<Course> getAllCourses();

public List<Course> getAllCoursesByCategory(String category);

public List<Course> getAllCoursesWhichNoProposal( );

}

CourseServiceImpl.java

@Service
public class CourseServiceImpl implements CourseService {
@Autowired
private CoursesDAO coursesDAOImpl;


@Transactional
public Course findCourseById(Integer id) {

    Course course = coursesDAOImpl.findCourseById(id);

    return course;
}
@Transactional
public Course updateCourse(Course course) {
    course = coursesDAOImpl.updateCourse(course);

    return course;
}
@Transactional
public Course createCourse(Course course) {

    course = coursesDAOImpl.createCourse(course);

    return course;
}
@Transactional
public List<Course> getAllCourses() {

    List<Course> listCourses = coursesDAOImpl.getAllCourses();

    return listCourses;
}
@Transactional
public List<Course> getAllCoursesByCategory(String category) {

    List<Course> listCoursesByCategory = coursesDAOImpl
            .getAllCoursesByCategory(category);

    return listCoursesByCategory;
}

@Transactional
public List<Course> getAllCoursesWhichNoProposal( ) {

    List<Course> listCoursesNoProposal = coursesDAOImpl
            .getAllCoursesWhichNoProposal( );

return listCoursesNoProposal;
}

@Transactional
public void deleteCourse(Course course) {
    coursesDAOImpl.deleteCourse(course);

};

application-contex.xml

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

<mvc:annotation-driven />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close"
    p:driverClassName="org.h2.Driver"
    p:url="jdbc:h2:tcp://localhost:9092/~/QWE;INIT=create schema if not exists QWE\;"
    p:username="sa"
    p:password="" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">   
    <props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop> 
<prop key="hibernate.show_sql">true</prop> 

<prop key="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop> 
<prop key="hibernate.default_schema">QWE</prop>
</props>    
</property> 

<property name="annotatedClasses">
  <list>
    <value>com.epam.edu.jtc.entity.User</value>
    <value>com.epam.edu.jtc.entity.Category</value>
    <value>com.epam.edu.jtc.entity.Course</value>
    <value>com.epam.edu.jtc.entity.UserCourse</value>
    <value>com.epam.edu.jtc.entity.ManagerCourse</value>
     </list>
 </property>
 </bean>        

<!-- FreeMarker Configuration -->
<bean id="freemarkerEmailConfig" class="freemarker.template.Configuration">
<property name="directoryForTemplateLoading" value="WEB-INF/pages/templates" />
<property name="objectWrapper">
    <bean class="freemarker.template.DefaultObjectWrapper"/>
</property>

</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
 <tx:annotation-driven transaction-manager="transactionManager"/>   
 <context:component-scan base-package="com.epam.edu.jtc" />

</beans>
1

There are 1 answers

3
Mudassar On

The problem as per my understanding lies with the way you are dealing with the session

getCurrentSession() : Obtains the current session.

openSession() : Creates a new session

Also using the below link is not recommended unless you have a strong reason

<prop key="hibernate.current_session_context_class">thread</prop>