3

I have Spring application which works with hibernate.The session for the hibernate is not creating.it throws error as below
[the console error][1]: https://i.sstatic.net/Mb0Ah.png
My dispatcher code as below

<?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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.1.xsd">


    <context:component-scan base-package="com.oi.controller" />



    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- data source -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        >
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:61391/springmvc2k" />
        <property name="username" value="adminHYMgZHE" />
        <property name="password" value="byIkcunaje5K" />
    </bean> 

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>

            </props>
        </property>
        <property name="packagesToScan" value="com.oi.bean"></property>
    </bean>
    <!-- The transaction manager -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

My bean class

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Entity;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Entity
@Table(name="users")
public class User {
    @Id
    @GeneratedValue
    int id;
    @Column(name = "username")
    String name;
    @Column(name = "password")
    String password;
    @Column(name = "email")
    String email;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

My spring controller as follow

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.oi.bean.User;

@Controller
public class BaseController {
    @Autowired
    private SessionFactory sessionFactory;

    @RequestMapping(value="/")
    public String getRespnse(Model m) {
        Session session =sessionFactory.getCurrentSession();
    //  List<User> usrLst=(List<User>) session.createQuery("from User");
    //  System.out.println("IN"+usrLst);
        return "home";
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}

Please help.how to proceed..!!!

3 Answers 3

6
  @RequestMapping(value="/")
  @Transactional
  public String getRespnse(Model m) {
        Session session =sessionFactory.getCurrentSession();
    //  List<User> usrLst=(List<User>) session.createQuery("from User");
    //  System.out.println("IN"+usrLst);
        return "home";
  }
  1. Add @Transactional with your controller method
  2. delete @EnableTransactionManagement to user class

Try it, i think it will work for you

2
  • Thanks. It worked. That is so great.can you explain about this two motioned statement. that would be so much helpful to me in future Commented Mar 28, 2016 at 11:53
  • what to import?
    – mercury
    Commented May 4, 2022 at 0:31
2

Add @Transactional attribute on the getRespnse(Model m) on your BaseController class and try as below.

@RequestMapping(value="/")
@Transactional
public String getRespnse(Model m) {
    Session session =sessionFactory.getCurrentSession();
1
  • @RajasekarRamalingam Transactional annotation allows to indicate Spring that we want to use its declarative transaction management feature and that is why we don't need to worry about creating a transaction, committing or rolling it back. And Hibernate looks for an active transaction while creating a new session. And regarding removing EnableTransactionManagement from User class - Spring configuration file already has <tx:annotation-driven....> which is equivalent to this annotation. No need to define it twice. Not sure, defining it twice would cause any issues,AFAIK,not tested though Commented Mar 28, 2016 at 12:03
0

in my case i added @Transactional annotation in the whole controller and it worked

Not the answer you're looking for? Browse other questions tagged or ask your own question.