2

I'm trying to make an application with Spring3 MVC and Hibernate, but something I'm doing wrong because every time I try to make a database query I have this error:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
    org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
    com.ibex.pBaseSpring.repository.hibernate.HibernateUsuarioDAO.login(HibernateUsuarioDAO.java:25)
    com.ibex.pBaseSpring.service.impl.UsuarioManagerImpl.login(UsuarioManagerImpl.java:23)
    com.ibex.pBaseSpring.web.LoginController.login(LoginController.java:32)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I tried all the solutions I found and I still get the same error, this is my code:

app-config.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <context:component-scan base-package="com.ibex.pBaseSpring.repository" />
    <context:component-scan base-package="com.ibex.pBaseSpring.service" />
    <context:component-scan base-package="com.ibex.pBaseSpring.web" />
    <context:annotation-config />
    <mvc:annotation-driven />

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

    <context:property-placeholder location="/WEB-INF/spring/hibernate.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName">
            <value>${hibernate.connection.driver_class}</value>
        </property>
        <property name="url">
            <value>${hibernate.connection.url}</value>
        </property>
        <property name="username">
            <value>${hibernate.connection.username}</value>
        </property>
        <property name="password">
            <value>${hibernate.connection.password}</value>
        </property>
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.ibex.pBaseSpring.domain" />
        <property name="hibernateProperties">
            <props>
                <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>

    <mvc:resources location="/resources/" mapping="/resources/**" />
</beans>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>pBaseSpring</display-name>
  <servlet>
    <servlet-name>pBaseSpring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/spring/app-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>pBaseSpring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

LoginController

package com.ibex.pBaseSpring.web;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ibex.pBaseSpring.domain.Usuario;
import com.ibex.pBaseSpring.service.UsuarioManager;

@Controller
public class LoginController {

    private UsuarioManager usuarioManager;

    @Inject
    public LoginController(UsuarioManager usuarioManager){
        this.usuarioManager = usuarioManager;
    }

    @RequestMapping(value="/inicio")
    public String inicio(){
        //comprobar que el usuario no esta registrado ya...
        return "login";
    }

    @RequestMapping(value="/login")
    public String login(@RequestParam("login") String login, @RequestParam("pass") String pass){
        System.out.println(login + " - " + pass);
        Usuario usuario = usuarioManager.login(login, pass);
        return "main";
    }

    @RequestMapping(value="/logout")
    public String logout(){
        return "inicio";
    }
}

UsuarioManager

package com.ibex.pBaseSpring.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ibex.pBaseSpring.domain.Usuario;
import com.ibex.pBaseSpring.repository.UsuarioDAO;
import com.ibex.pBaseSpring.service.UsuarioManager;
@Service
public class UsuarioManagerImpl implements UsuarioManager{

    @Autowired
    private UsuarioDAO usuarioDAO;
    public void setUsuarioDAO(UsuarioDAO usuarioDAO) {
        this.usuarioDAO = usuarioDAO;
    }
    public Usuario login(String login, String pass) {
        return usuarioDAO.login(login, pass);
    }
}

HibernateUsuarioDAO

package com.ibex.pBaseSpring.repository.hibernate;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ibex.pBaseSpring.domain.Usuario;
import com.ibex.pBaseSpring.repository.UsuarioDAO;

@Repository
public class HibernateUsuarioDAO implements UsuarioDAO{

    private SessionFactory sessionFactory;

    @Autowired
    public HibernateUsuarioDAO(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }

    @Transactional(readOnly = true)
    public Usuario login(String login, String pass) {
        Criteria c = sessionFactory.getCurrentSession().createCriteria(Usuario.class);
        c.add(Restrictions.eq("login", login));
        c.add(Restrictions.eq("pass", pass));
        return (Usuario) c.uniqueResult();
    }

}

I'm a little desperate, thanks for the help. Bye!!

2 Answers 2

2

You did not added tx in your bean. refere this bean tag

<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:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
">

and then add this line in your app-config.xml :

<tx:annotation-driven transaction-manager="transactionManager" />
Sign up to request clarification or add additional context in comments.

7 Comments

When I put this I get this error: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring/app-config.xml]: Initialization of bean failed;
nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.getAnnotation(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
make sure you added all jar files of spring of same version. I think you are having problem with jar files dependencies.
I don't know, because now the server will not find the RequestMapping WARNING: No mapping found for HTTP request with URI [/pBaseSpring/inicio] in DispatcherServlet with name 'pBaseSpring'
|
1

You missed <tx:annotation-driven />, @Transactional won't work without it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.