简体   繁体   中英

Spring MVC CRUD operation using hibernate tempate integration by annotation

I created a user registration project in my Eclipse IDE and I use hibernate template for my crud operation using annotation now I am getting these session factory required error what should I do?

my web.xml file

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://xmlns.jcp.org/xml/ns/javaee"   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"  version="3.1">
  <display-name>SpringMvcCrud</display-name>
    <servlet>
     <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

my dispatcher-servlet.xml file

     <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:component-scan base-package="controller" />
<bean
     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property> 
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"  />
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
    <property name="username" value="system" />
    <property name="password" value="rrr" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <array>
            <value>beans</value>
        </array>
    </property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"
    autowire="constructor" />

<bean id="userservice" class="service.UserServiceImpl" />
<bean id="userdao" class="dao.UserDaoImpl" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource" /> 
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

</beans>

MY UserControllerclass

         package controller;
 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import beans.User;
import service.UserService;

@Controller
public class UserRegistration {
@Autowired
UserService userservice;

@RequestMapping(value="/registration", method=RequestMethod.GET)
public String userdisplay(){
        return "registration";
}
@RequestMapping(value="/doregistration", method=RequestMethod.POST)
public ModelAndView doregistration(@ModelAttribute("user") User user,  BindingResult result){
    ModelAndView mod=null;
    if (result.hasErrors()){
        mod=new ModelAndView("registration");
        return mod;
    }
    else{
        mod=new ModelAndView("success");
        userservice.save(user);
        return mod;
    }   
 }
}

my User.java file

package beans;
import javax.persistence.*;

@Entity
public class User {
@Id 
@Column()
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQUENCE1")
@SequenceGenerator(name="SEQUENCE1", sequenceName="User_seq",  allocationSize=1)
private String id;
@Column()
private String name;
@Column()
private String password;

public User(){}

public User(String id, String name, String password) {
    super();
    this.id = id;
    this.name = name;
    this.password = password;
}
public String getId() {
    return id;
}
public String getName() {
    return name;
}
public String getPassword() {
    return password;
}
public void setId(String id) {
    this.id = id;
}
public void setName(String name) {
    this.name = name;
}
public void setPassword(String password) {
    this.password = password;
}
}

My UserDao.java file

package dao;

import beans.User;

public interface UserDao {
    public void save (User user);
}

My UserDaoImpl file

package dao;

import org.springframework.beans.factory.annotation.*;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import beans.User;
@Repository
public class UserDaoImpl implements UserDao {

@Autowired
HibernateTemplate hibernateTemplate;

@Override
public void save(User user) {       
    hibernateTemplate.save(user);
}
}

my UserService file

package service;

import beans.User;

    public interface UserService {
    public void save(User user);
}

my UserServiceImpl

package service;

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

import beans.User;
import dao.UserDao;
@Service
public class UserServiceImpl implements UserService {

@Autowired
UserDao userdao;

@Override
public void save(User user) {

userdao.save(user);
    }
}

And these are my serverlog

1

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userservice': Unsatisfied dependency expressed through field 'userdao': Error creating bean with name 'userdao': Unsatisfied dependency expressed through field 'hibernateTemplate': Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userdao': Unsatisfied dependency expressed through field 'hibernateTemplate': Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userdao': Unsatisfied dependency expressed through field 'hibernateTemplate': Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required

2

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required

3

Caused by: java.lang.IllegalArgumentException: Property 'sessionFactory' is required

HibernateTemplate(doc) requires sessionFactory while creating bean.

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

I think by injecting the sessionFactory in the hibernateTemplate would fix your problem. But you can completly get rid of hibernateTemplate. Try the following, It seems a better approch to me than using hibernateTemplate

UserDaoImpl file

package dao;

import org.springframework.beans.factory.annotation.*;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import beans.User;
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void save(User user) {       
        sessionFactory.getCurrentSession().save(user);
    }
}

UserServiceImpl file

package service;

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

import beans.User;
import dao.UserDao;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userdao;

    @Transactional
    @Override
    public void save(User user) {

        userdao.save(user);
    }
}

dispatcher-servlet.xml file

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    // Put all the entries in a similar way
    //
        <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory" />

        <tx:annotation-driven transaction-manager="transactionManager" />
</beans>        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM