'I am trying to perform an insert operation using spring MVC + ORM but getting this error

Getting this error when try to insert new data into database, please check if someone could help

Thankyou

ERROR:

SEVERE: Context initialization failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeController': Unsatisfied dependency expressed through field 'studentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentService': Unsatisfied dependency expressed through field 'studentDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentDao': Unsatisfied dependency expressed through field 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Cannot resolve reference to bean 'factory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'factory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1425) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)

Spring-servlet.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:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="  
        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  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">  
  
    <tx:annotation-driven/>
    <!-- Provide support for component scanning -->  
    <context:component-scan base-package="com.spring" />   
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/views/"></property>  
        <property name="suffix" value=".jsp"></property>          
    </bean>  
    
   <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id = "ds">
    <property name = "driverClassName" value = "com.mysql.cj.jdbc.Driver"/>
    <property name = "url" value = "jdbc:mysql://localhost:3306/springjdbc"/>
    <property name= "username" value ="Shubham" />
    <property name = "password" value = "1122"/>
 </bean>
 
 <bean class = "org.springframework.orm.hibernate5.LocalSessionFactoryBean" id = "factory">
    <property name = "dataSource" ref="ds"></property>
    <property name = "hibernateProperties">
        <props>
            <prop key = "hibernate.dilect">org.hibernate.dialect.MySQL57Dialect</prop>
            <prop key = "hibernate.show_sql">true</prop>
            <prop key = "hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="annotatedClasses">
    <list>
        <value>
            com.spring.Model.Student
        </value>
    </list>
    </property>
 </bean>
 
 <bean class="org.springframework.orm.hibernate5.HibernateTemplate" id = "hibernateTemplate">
    <property name = "sessionFactory" ref="factory"/>
 </bean>
 
 <!-- <bean class = "com.spring.dao.StudentDao" name = "studentDao">
    <property name = "hibernateTemplate" ref="hibernateTemplate"/>
 </bean> -->
 
 
 <bean class = "org.springframework.orm.hibernate5.HibernateTransactionManager" id = "transactionManager">
    <property name = "sessionFactory" ref="factory"></property>
 </bean>
    

</beans> 

Pom.xml:

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spring.crud</groupId>
  <artifactId>crudOperations</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>crudOperations Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.19.RELEASE</version>
    </dependency>
    
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.3.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>


    
    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>crudOperations</finalName>
  </build>
</project>

HomeController.java:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.spring.Model.Student;
import com.spring.service.StudentService;

@Controller
public class HomeController {
    
    @Autowired
    private StudentService studentService;
    
    @RequestMapping("/home")
    public String home() 
    {
        
        return "home";
    }
    
    @RequestMapping(path ="/processform", method = RequestMethod.POST)
    public String processformHandle(@ModelAttribute Student student, Model model)
    {
        System.out.println("Working fine "+ student); 
        this.studentService.createStudent(student);
        
        return "success";
    }

    
}

StudentService.java:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.Model.Student;
import com.spring.dao.StudentDao;

@Service
public class StudentService {
    
    @Autowired
    private StudentDao studentDao;
    public StudentDao getStudentDao() {
        return studentDao;
    }
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
    public StudentService(StudentDao studentDao) {
        super();
        this.studentDao = studentDao;
    }
    public StudentService() {
        super();
        // TODO Auto-generated constructor stub
    }
    public int createStudent(Student student)
    {
        return this.studentDao.saveStudent(student);
    }
}

StudentDao.java:




import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.spring.Model.Student;

@Repository
public class StudentDao {
    
    @Autowired
    private HibernateTemplate hibernateTemplate;
    
    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public StudentDao() {
        super();
        // TODO Auto-generated constructor stub
    }

    public StudentDao(HibernateTemplate hibernateTemplate) {
        super();
        this.hibernateTemplate = hibernateTemplate;
    }

    @Transactional
    public int saveStudent(Student student)
    {
        int save =(Integer) this.hibernateTemplate.save(student);
        return save;
    }
}

Student.java:


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Students")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String email;
    private String userName;
    private String password;
    @Override
    public String toString() {
        return "Student [id=" + id + ", email=" + email + ", userName=" + userName + ", password=" + password + "]";
    }
    public Student(String email, String userName, String password) {
        super();
        this.email = email;
        this.userName = userName; 
        this.password = password;
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

here is the project Structure enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source