I am new to Java/Spring/Eclipse. I am working on creating the login screen using Apache Tomcat 7, SQL Server, Spring MVC, and Hibernate. When the repository layer tries to create the Hibernate session, the system crashes. I am at a lose as to why. Here is the error:
The Repository:PHP 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)
The EntityPHP Code:package com.accumed.protracking.repository;
@Repository("userInfoRepository")
@Transactional
public class JpaUserInfoRepository implements UserInfoRepository {
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;
}
}
The ServicePHP 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;
// snipped code
@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;
}
// snipped code
}
The ControllerPHP Code:package com.accumed.protracking.service;
@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;
}
}
-------------- CONFIGURATION ----------------PHP Code:package com.accumed.protracking;
@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";
}
}
}
The web.xml
The root-context.xmlHTML 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>HTML 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 --> <context:component-scan base-package="com.accumed.protracking.service"/> <tx:annotation-driven/> <context:component-scan base-package="com.accumed.protracking.repository" /> <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> </beans>


Reply With Quote