Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF 2.0, Spring 3.1 and Hibernate 4.0 integration
    primarykey
    data
    text
    <p>I am trying setup a web application with JSF 2.0 + Hibernate 4 and Spring 3.1. I am getting the following Exception when I test my Dao method.</p> <pre><code> Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.springHibernate.dao.EmpDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Error applying BeanValidation relational constraints at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:83) at com.springHibernate.test.TestEmployee.main(TestEmployee.java:15) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.springHibernate.dao.EmpDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Error applying BeanValidation relational constraints at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) ... 13 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Error applying BeanValidation relational constraints at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) ... 15 more Caused by: org.hibernate.HibernateException: Error applying BeanValidation relational constraints at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:219) at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:126) at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:295) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1726) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1764) at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184) at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) ... 25 more Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:208) ... 33 more Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at org.hibernate.validator.util.LoggerFactory.make(LoggerFactory.java:29) at org.hibernate.validator.util.Version.&lt;clinit&gt;(Version.java:24) at org.hibernate.validator.engine.ConfigurationImpl.&lt;clinit&gt;(ConfigurationImpl.java:59) at org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:41) at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:269) at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111) at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:521) at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:119) ... 38 more </code></pre> <p>Below is my code</p> <p><strong>Employee.java</strong></p> <pre><code>package com.springHibernate.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="employee") public class Employee { @Id @Column(name="EmpID") private int empID; @Column(name="EmpName") private String empName; @Column(name="Email") private String email; public int getEmpID() { return empID; } public void setEmpID(int empID) { this.empID = empID; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } </code></pre> <p>EmployeeDao.java</p> <pre><code>package com.springHibernate.dao; import java.util.List; import com.springHibernate.model.Employee; public interface EmpDao { public List&lt;Employee&gt; getAllEmployees(); } </code></pre> <p><strong>EmployeeDaoImpl.java</strong></p> <pre><code>package com.springHibernate.dao; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.springHibernate.model.Employee; @Repository("empDaoImpl") public class EmpDaoImpl implements EmpDao{ @Autowired private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @SuppressWarnings("unchecked") @Override public List&lt;Employee&gt; getAllEmployees() { return sessionFactory.getCurrentSession().createQuery(" from Employee").list(); } } </code></pre> <p><strong>EmpService.java</strong></p> <pre><code>package com.springHibernate.service; import java.util.List; import com.springHibernate.model.Employee; public interface EmpService { public List&lt;Employee&gt; getAllEmployees(); } </code></pre> <p>EmpServiceImpl.java</p> <pre><code>package com.springHibernate.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.springHibernate.dao.EmpDao; import com.springHibernate.model.Employee; @Service("empServiceImpl") public class EmpServiceImpl implements EmpService { @Autowired EmpDao empDao; public EmpDao getEmpDao() { return empDao; } public void setEmpDao(EmpDao empDao) { this.empDao = empDao; } @Transactional(readOnly = true) public List&lt;Employee&gt; getAllEmployees() { return empDao.getAllEmployees(); } } </code></pre> <p><strong>EmployeeBean.java</strong></p> <pre><code>package com.springHibernate.managedBeans; import java.util.ArrayList; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.RequestScoped; import org.springframework.beans.factory.annotation.Autowired; import com.springHibernate.model.Employee; import com.springHibernate.service.EmpService; @ManagedBean(name = "empBean") @RequestScoped public class EmployeeBean { @ManagedProperty(value = "#{EmpServiceImpl}") EmpService empService; List&lt;Employee&gt; empList; private int empID; private String empName; private String email; public EmpService getEmpService() { return empService; } public void setEmpService(EmpService empService) { this.empService = empService; } public List&lt;Employee&gt; getEmpList() { empList = new ArrayList&lt;Employee&gt;(); empList.addAll(getEmpService().getAllEmployees()); return empList; } public void setEmpList(List&lt;Employee&gt; empList) { this.empList = empList; } public int getEmpID() { return empID; } public void setEmpID(int empID) { this.empID = empID; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } </code></pre> <p><strong>applicationContext.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt; &lt;context:component-scan base-package="com.springHibernate.dao,com.springHibernate.managedBeans,com.springHibernate.service,com.springHibernate.model" /&gt; &lt;context:annotation-config /&gt; &lt;bean id="Employee" class="com.springHibernate.model.Employee" /&gt; &lt;bean id="EmpService" class="com.springHibernate.service.EmpServiceImpl"&gt; &lt;property name="empDao" ref="EmpDao"/&gt; &lt;/bean&gt; &lt;bean id="EmpDao" class="com.springHibernate.dao.EmpDaoImpl"&gt; &lt;property name="sessionFactory" ref="SessionFactory"&gt;&lt;/property&gt; &lt;/bean&gt; &lt;bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt; &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt; &lt;property name="url" value="jdbc:mysql://localhost:3306/test" /&gt; &lt;property name="username" value="root" /&gt; &lt;property name="password" value="ASSET" /&gt; &lt;/bean&gt; &lt;bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt; &lt;property name="dataSource" ref="DataSource" /&gt; &lt;property name="hibernateProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt; &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="txManager" /&gt; &lt;bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="SessionFactory" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p><strong>web.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt; &lt;display-name&gt;SpringHibernate&lt;/display-name&gt; &lt;context-param&gt; &lt;param-name&gt;javax.faces.CONFIG_FILES&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/faces-config.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;context-param&gt; &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt; &lt;param-value&gt;Development&lt;/param-value&gt; &lt;/context-param&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;/pages/index.xhtml&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;servlet&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;*.jsf&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;*.faces&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;context-param&gt; &lt;description&gt;State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2&lt;/description&gt; &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt; &lt;param-value&gt;client&lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;param-name&gt;javax.servlet.jsp.jstl.fmt.localizationContext&lt;/param-name&gt; &lt;param-value&gt;resources.application&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;com.sun.faces.config.ConfigureListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt; </code></pre> <p><strong>faces-config.xml</strong></p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xi="http://www.w3.org/2001/XInclude" 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-facesconfig_2_0.xsd"&gt; &lt;application&gt; &lt;el-resolver&gt;org.springframework.web.jsf.el.SpringBeanFacesELResolver&lt;/el-resolver&gt; &lt;/application&gt; &lt;/faces-config&gt; </code></pre> <p><strong>Testclass</strong></p> <pre><code>package com.springHibernate.test; import java.util.ArrayList; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.springHibernate.model.Employee; import com.springHibernate.service.EmpService; public class TestEmployee { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); EmpService empService = (EmpService)ctx.getBean("empServiceImpl"); List&lt;Employee&gt; l1 = new ArrayList&lt;Employee&gt;(); l1 = empService.getAllEmployees(); for (Employee e1 : l1) System.out.println("Employee Name: "+e1.getEmpName()); } } </code></pre> <p>I am trying to use annotations. Slf4j is already present in lib folder. Please have a look at the code and help me to setup the project.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload