I am new to Spring and I am trying to create a login page that uses Apache Tomcat 7, SQL Server, Spring MVC, and Hibernate 3. I have everything up and running, except for Hibernate is throwing an error while trying to create a session:
Code:
message Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
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.accumed.protracking.repository.JpaUserInfoRepository.get(JpaUserInfoRepository.java:37)
com.accumed.protracking.service.UserInfoServiceImpl.login(UserInfoServiceImpl.java:20)
com.accumed.protracking.LoginController.loginAttempt(LoginController.java:36)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Here are my class and configuration:
Repository class
-------------------
Code:
package com.accumed.protracking.repository;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.accumed.protracking.domain.UserInfo;
@Repository("userInfoRepository")
@Transactional
public class JpaUserInfoRepository implements UserInfoRepository {
// @Autowired
// private SessionFactory sessionFactory;
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource(name="sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public UserInfo get(String domain, String username, String password) {
Session session = sessionFactory.getCurrentSession();
Query query = session.getNamedQuery("callUserInfoGetProcedure")
.setParameter("domain", domain)
.setParameter("username", username)
.setParameter("password", password);
List result = query.list();
if(result.size() > 0)
return (UserInfo) result.get(0);
return null;
}
}
Entity Class
-------------------
Code:
package com.accumed.protracking.domain;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.*;
import org.hibernate.validator.constraints.NotEmpty;
@NamedNativeQueries({
@NamedNativeQuery(
name = "callUserInfoGetProcedure",
query = "exec UserInfoGet :domain, :username, :password",
resultClass = UserInfo.class
)
})
@Entity
@Table(name = "userInfo")
public class UserInfo {
@NotEmpty
private String domainName;
@NotEmpty
private Boolean corpActive;
@NotEmpty
private int tblCorporation_Id;
@Id
@NotEmpty
private int tblUser_Id;
@NotEmpty
private Boolean active;
@NotEmpty
private int admin;
@NotEmpty
private String username;
@NotEmpty
private Boolean ForcePasswordUpdate;
@NotEmpty
private String nameFirst;
@NotEmpty
private String nameLast;
@NotEmpty
private int use_PT_CO_Ind;
@NotEmpty
private int serviceLevel;
@Column(name = "DomainName")
public String getDomainName() {
return domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
@Column(name = "CorpActive")
public Boolean getCorpActive() {
return corpActive;
}
public void setCorpActive(Boolean corpActive) {
this.corpActive = corpActive;
}
@Column(name = "tblCorporation_Id")
public int getTblCorporation_Id() {
return tblCorporation_Id;
}
public void setTblCorporation_Id(int tblCorporation_Id) {
this.tblCorporation_Id = tblCorporation_Id;
}
@GeneratedValue(strategy = IDENTITY)
@Column(name = "tblUser_Id")
public int getUserId() {
return tblUser_Id;
}
public void setUserId(int tblUser_Id) {
this.tblUser_Id = tblUser_Id;
}
@Column(name = "Active")
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
@Column(name = "admin")
public int getAdmin() {
return admin;
}
public void setAdmin(int admin) {
this.admin = admin;
}
@Column(name = "User_Id")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "ForcePasswordUpdate")
public Boolean getForcePasswordUpdate() {
return ForcePasswordUpdate;
}
public void setForcePasswordUpdate(Boolean forcePasswordUpdate) {
ForcePasswordUpdate = forcePasswordUpdate;
}
@Column(name = "NameFirst")
public String getFirstName() {
return nameFirst;
}
public void setNameFirst(String nameFirst) {
this.nameFirst = nameFirst;
}
@Column(name = "NameLast")
public String getLastName() {
return nameLast;
}
public void setLastName(String nameLast) {
this.nameLast = nameLast;
}
@Column(name = "Use_PT_CO_Ind")
public int getUse_PT_CO_Ind() {
return use_PT_CO_Ind;
}
public void setUse_PT_CO_Ind(int use_PT_CO_Ind) {
this.use_PT_CO_Ind = use_PT_CO_Ind;
}
@Column(name = "getServiceLevel")
public int getServiceLevel() {
return serviceLevel;
}
public void setServiceLevel(int serviceLevel) {
this.serviceLevel = serviceLevel;
}
}
Service
-------------------
Code:
package com.accumed.protracking.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.accumed.protracking.domain.UserInfo;
import com.accumed.protracking.repository.UserInfoRepository;
@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {
@Autowired
private UserInfoRepository userInfoRepository;
@Override
public UserInfo login(String domain, String username, String password)
throws AuthenticationException {
UserInfo userInfo = this.userInfoRepository.get(domain, username, password);
if (userInfo == null) {
throw new AuthenticationException("Wrong domain/username/password combination.", "invalid.username");
}
return userInfo;
}
}
Controller Class
-------------------
Code:
package com.accumed.protracking;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.accumed.protracking.domain.UserInfo;
import com.accumed.protracking.service.AuthenticationException;
import com.accumed.protracking.service.UserInfoService;
@Controller
@RequestMapping(value = "/login")
public class LoginController {
public static final String ACCOUNT_ATTRIBUTE = "account";
@Autowired
private UserInfoService userInfoService;
@RequestMapping(method = RequestMethod.GET)
public String initial()
{
return "login";
}
@RequestMapping(method = RequestMethod.POST)
public String loginAttempt(@RequestParam String domain, @RequestParam String username, @RequestParam String password,
RedirectAttributes redirect, HttpSession session) throws AuthenticationException {
try {
UserInfo userInfo = this.userInfoService.login(domain, username, password);
session.setAttribute(ACCOUNT_ATTRIBUTE, userInfo);
return "redirect:/index.htm";
} catch (AuthenticationException ae) {
redirect.addFlashAttribute("exception", ae);
return "redirect:/login";
}
}
}
Web.xml
-------------------
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Application Context
-------------------
Code:
<?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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
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-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver:***********;databaseName=***********"/>
<property name="username" value="***********"/>
<property name="password" value="***********"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.accumed.protracking.domain"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven/>
<context:component-scan base-package="com.accumed.protracking.repository" />
</beans>
Servlet context
-------------------
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.accumed.protracking" />
</beans:beans>