How to make Spring managed beans transactional - spring

I'm working with JSF. Recently we decided to exclude service layer from our project, cause this layer in 80% of cases consist only of recalling the same named dao-methods (definelty, I know, that this way is "not true"). So, now we need to make our managed beans layer transactional, and here is the problem: I can make dao-layer transactional, but when I try to move this duty to managegbeans layer, I get an
org.hibernate.HibernateException: No Session found for current thread
Dao and managedbeans layers are in separated modules, so they have separated context.xml files. When i define tx:annotation-driven tag in dao's context.xml file, #Transactional annotations in classes in dao-module are "visible", but when I try to define this tag in managedbeans context.xml file, none of the #Transactional annotations (neiter the dao, nor the managedbeans) are visible, so i get an exception.
Any ideas, how I can correctly configure transactions?
applicationContext-dao.xml
<jee:jndi-lookup id="dataSource" jndi-name="java:/MSSQLDataSource"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.company.project.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<context:component-scan base-package="com.company.project.dao"/>
applicationContext-web.xml
<context:annotation-config />
//Just because of the service layer is not fully removed, de facto we are using this import only for imort dao-context
<import resource="classpath:/applicationContext-service.xml"/>
<context:component-scan base-package="com.company.project"/>
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="false"/>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
Part of web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/applicationContext-web.xml
classpath:/applicationContext-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.company.project.util.ServletContextListenerConfig</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
ServletContextListenerConfig class:
public class ServletContextListenerConfig implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");
}
public void contextDestroyed(ServletContextEvent event) {
// NOOP
}
}
Thanks in advance!
UPDATE:
After reading a "couple" of articles, I've spoken, that problem may be in double definition beans because of mapping them by different <context: component-scan>. In most of articles I've read, this contexts were root context by contextConfigLocation and servlet-context by DispatcherServlet , but in my case I haven't got DispatcherServlet, but I have FacesServlet by javax.faces.webapp.FacesServlet. So, does Faces Servlet do the same thing and, if yes, how? I don't see any connection between Faces Servlet and my context.xml files.
UPDATE 2:
My fault, that I didn't show this code at once.
Here's my controller:
#Component
#ViewScoped
public class ProfileBean extends EntityBeanBase<Profile> {
#Autowired
private ProfileDao profileDao;
#Override
#Transactional
protected List<Profile> getInitEntities() {
return profileDao.getAll();
}
public void profileNameValidator(FacesContext context, UIComponent component, Object value) {
if (!selectedEntity.getName().equals(value) && profileDao.exists((String) value)) {
System.out.println("Error validating");
}
}
EntityBenBase consist of many-many methods, but now I'm using only getInitEntities(), which is abstract in EnityBeanBase.
And ProfileDaoImpl:
#Repository
public class ProfileDaoImpl extends BaseDao implements ProfileDao {
#Override
public List<Profile> getAll() {
return sessionFactory.getCurrentSession().createCriteria(Profile.class).list();
}
..........
}
BaseDao is simple:
public abstract class BaseDao {
#Autowired
public SessionFactory sessionFactory;
}

Related

springmvc and url-pattern

web.xml
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
controller
#Controller
#RequestMapping("/car/*")
public class CarController extends BaseController {
#RequestMapping("baojia.html")
public ModelAndView baojia() {
ModelAndView view = new ModelAndView();
view.setViewName("baojia");
return view;
}
when i visit http://mydomain/car/baojia.html and has this error:
[carloan]2016-04-21 09:01:31,177 WARN [org.springframework.web.servlet.PageNotFound] - <No mapping found for HTTP request with URI [/views/baojia.jsp] in DispatcherServlet with name 'springMVC'>
spring.xml ViewResolver
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="false"/>
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
and i have file in /views/boajia.jsp
whether i writer, it don't work
<mvc:resources mapping="/views/" location="/views/**" />
and i have another question, i wan't to matching this url-pattern: /api/*
and the controller is:
#Controller
#RequestMapping("/api/*")
public class CarApiController extends BaseController {
#RequestMapping("get")
#ResponseBody
public JsonResult getCars()
but it can't work
try #RequestMapping("/car") instead of #RequestMapping("/car/*")
And check below two links to understand, how request mapping defined.
can anybody explain me difference between class level controller and method level controller..?
http://duckranger.com/2012/04/advanced-requestmapping-tricks-controller-root-and-uri-templates/
URL mapping declaration is not proper use #RequestMapping("/car") and #RequestMapping("/baojia.html")

Hibernate Spring Integration, Tables Are Being Created, But SessionFactory is not Autowired

I have integrated Spring and Hibernate, when i run my application using jetty plugin for maven, Hibernate creates tables but i my sessionFactory object is null,
web.xml
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value></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,
/WEB-INF/spring/security-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>
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" 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.fyp.ptma" />
<beans:bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"></beans:property>
<beans:qualifier value="sessionFactory"></beans:qualifier>
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.fyp.ptma.model.Application</beans:value>
<beans:value>com.fyp.ptma.model.ApplicationVersion</beans:value>
<beans:value>com.fyp.ptma.model.Defect</beans:value>
<beans:value>com.fyp.ptma.model.DefectDetail</beans:value>
<beans:value>com.fyp.ptma.model.Group</beans:value>
<beans:value>com.fyp.ptma.model.Downloader</beans:value>
<beans:value>com.fyp.ptma.model.Notification</beans:value>
<beans:value>com.fyp.ptma.model.RegisterStatus</beans:value>
<beans:value>com.fyp.ptma.model.User</beans:value>
<beans:value>com.fyp.ptma.model.Invitation</beans:value>
<beans:value>com.fyp.ptma.model.Installation</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">create</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
UserDao.java
#Repository
public class UserDao implements IUserDao {
#Autowired
#Qualifier(value="sessionFactory")
private SessionFactory sessionFactory;
public Session openSession() {
return sessionFactory.getCurrentSession();
}
public Long save(Object objToSave) throws PersistenceException,
ConstraintViolationException {
Session session = null;
try {
session = this.openSession();
session.beginTransaction();
session.save(objToSave);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
// session.close();
}
return null;
}
public Long update(Object objToUpdate) throws PersistenceException {
// TODO Auto-generated method stub
return null;
}
public boolean delete(Object objToDelete) throws PersistenceException,
ConstraintViolationException {
// TODO Auto-generated method stub
return false;
}
public List<Application> getAllApplications(User user)
throws HibernateException {
// TODO Auto-generated method stub
return null;
}
public Application getApplication(User user) throws HibernateException {
// TODO Auto-generated method stub
return null;
}
public boolean authenticateLogin(User user) throws AuthenticationException,
HibernateException {
Session session = this.openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.eq("email", user.getEmail()));
User userDB = (User) criteria.uniqueResult();
return userDB.getPassword().equals(user.getPassword());
}
public User getUserByEmail(String email) throws HibernateException {
Session session = this.openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.eq("email", email));
User userDB = (User) criteria.uniqueResult();
// session.close();
return userDB;
}
}
My Controller
#ResponseBody
#RequestMapping(value = "/processRegistration.html", method = RequestMethod.POST)
public String processRegistration(#ModelAttribute("user") User user,
Model model, BindingResult result) {
UserDao uDao = DaoFactory.getUserDao();
uDao.save(user);
return "ok";
}
when i call uDao.save in my controller , sessionFactory object of UserDao is null, but during initialisation i dont get any wiring error when i try my controller with below line,
ApplicationContext context = new
AnnotationConfigApplicationContext("com.fyp.ptma.dao");
it throw exception while submitting form:
: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
It is stupid but what i was doing i was AutoWired my SessionFactory, and Dao Class but when i was getting Dao from Dao Factory i was returning as return new Dao(); this was causing to create whole new object, because SessionFactory was instance variable in Dao, it get default value of Object which was null here, below is my bug snipped
public class UserDao{
#AutoWired
private SessionFactory sessionFactory;
...
//more code
}
public static Dao getUserDao() {
return new UserDao();
}

Internationalization in spring 3.1.2 using annotation not working

I am using spring 3.1.2 MVC.
I am trying to configure web application by annotations
Here I want to implement internationalization for that I am using LocaleChangeInterceptor , ReloadableResourceBundleMessageSource ,SessionLocaleResolver classes.
here is my config class :
#Configuration
#EnableWebMvc
public class MyAppConfig extends WebMvcConfigurerAdapter{
#Bean
public ReloadableResourceBundleMessageSource resourceBundleMessageSource(){
ReloadableResourceBundleMessageSource messageSource=new ReloadableResourceBundleMessageSource();
String[] resources= {"classpath:labels","classpath:message"};
messageSource.setBasenames(resources);
return messageSource;
}
#Bean
public LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("locale");
return localeChangeInterceptor;
}
#Bean
public SessionLocaleResolver sessionLocaleResolver(){
SessionLocaleResolver localeResolver=new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("da","DK"));
return localeResolver;
}
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
But this is not working it shows default text for
<spring:message code="login.user.password" text="Password" />
as "Password".
please help me.
where as when I do following configuration in applicationContext.xml it works fine.
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:message</value>
<value>classpath:labels</value>
</list>
</property>
<property name="defaultEncoding" value="ISO-8859-1" />
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="locale"/>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="da_DK" />
</bean>
Please help me to solve this issue.
The bean name for messageSource has to be "messageSource", with #Configuration you have it as resourceBundleMessageSource. Change it to this:
#Bean
public ReloadableResourceBundleMessageSource messageSource(){
ReloadableResourceBundleMessageSource messageSource=new ReloadableResourceBundleMessageSource();
String[] resources= {"classpath:labels","classpath:message"};
messageSource.setBasenames(resources);
return messageSource;
}
i resolved my problem , i was missing init param entry in my web.xml
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.config.AppConfig
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
after adding this entry my config with annotation is working fine. :)

I am trying to use spring application context and spring di in my scala vaadin app, but can't get a datasource to be injected

i am new to scala and vaadin, i'm just experimenting. I am trying to use spring application context and spring di in my scala vaadin app, but can't get a datasource to be injected. I dont know, maybe i'm doing something fundamentally wrong but here's my code:
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<description>
Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>
<servlet>
<servlet-name>Scalatest Application</servlet-name>
<servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
<init-param>
<description>Vaadin application class to start</description>
<param-name>application</param-name>
<param-value>com.example.scalatest.ScalaApp</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Scalatest Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
application context:
<bean id="main" class="com.example.scalatest.ScalaApp">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="url"/>
<property name="username" value="root"/>
<property name="password" value="pass"/>
</bean>
and in my scala class
var dataSource:DataSource = _;
def setDataSource(datasource:DataSource){
dataSource = datasource;
}
Isn't working, ds is null at launch.
Can anyone guide me please?
The main problem you have is your application (ScalaApp) is not instantiated by Spring container but by VaadinServlet - make sure it does. There are several strategies. Here is the example project that can help you: https://github.com/archiecobbs/dellroad-stuff-vaadin-spring-demo3
Few more pieces of advice...
Instead of writing a setter yourself, add #BeanProperty annotation to your variable. Scala compiler will generate setter and getter for your variable:
#BeanProperty private var dataSource:DataSource = _
There is even better way - using Spring's annotation based container configuration. If you have only one bean of type DataSource in your context, simply add #Autowired to your variable (no need for xml definition in the context file - your class should be annotated as a #Component ):
#Component
class ScalaApp {
#Autowired private var dataSource:DataSource = _
}
Here is more information: http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-annotation-config

How Can I Pass jdbc.properties to Spring/Hibernate?

I am getting the following error while trying to pass in values from a properties file to Spring, so that I don't have to supply them directly in hibernate.cfg.xml. Is there a better (and correct) approach? I know that the properties file is being referenced because if I put in in invalid password, it fails on that. I'd be grateful for any help.
WARNING: No connection properties specified - the user must supply JDBC connections
Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426)
This is applicationContext.xml:
<context:component-scan base-package="cmsutil"/>
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configurationClass="org.hibernate.cfg.AnnotationConfiguration"
p:packagesToScan="cmsutil.*">
<property name="exposeTransactionAwareSessionFactory" value="false" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
</props>
</property>
</bean>
<bean id="txnManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
This is my hibernate.cfg.xml file:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="cmsutil.entity/contentcomponent.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Per Ramesh's request - web.xml
<web-app version="3.0" 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_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
For Ramesh, this is my jdbc.properties, located in the root directory (/src/java/jdbc.properties)
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://xx:3306/x
jdbc.username=y
jdbc.password=z
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.generate_statistics=false
add this bean in your applicationContext.xml
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>classpath:jdbc.properties</value></property>
</bean>
update this in web.xml instead of listener
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
I had the same issue but it was the result of an error in my java code. It required calling configure() on the Configuration before calling buildSessionFactory().
The javadoc for configure() method:
Use the mappings and properties specified in an application resource
named hibernate.cfg.xml.
Broken Code:
public static void main(String[] args) {
Configuration cfg = new Configuration();
factory = cfg.buildSessionFactory(); // missing the configure() call
Session s = factory.openSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
Solution:
public static void main(String[] args) {
Configuration cfg = new Configuration();
factory = cfg.configure().buildSessionFactory();
Session s = factory.openSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
Note: I was not using a Spring config.

Resources