嵌套异常是java.lang.IllegalArgumentException:未知实体:com.spring.services.FormData

时间:2019-04-08 09:56:28

标签: spring hibernate

我正在尝试从数据库中插入记录,但出现上述错误。 请建议打开任何solution.form,当我提交上述错误值时出现

控制器类:

package com.spring.form;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
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.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.spring.services.FormData;
import com.spring.services.FormDataService;

import javax.servlet.http.HttpServletResponse;


@Controller
public class FrontController
{
    @Autowired
    private FormDataService formDataService;


    HashMap<String, String> map=new HashMap<String, String>();

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping(value = {"/index"}, method = RequestMethod.GET)
    public String redirectIndex(Model model){

        model.addAttribute("formData",new FormData());
        return "index";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String model(Model model){

        //FrontBean fBean=new FrontBean();
        model.addAttribute("formData",new FormData());
        return "form";
    }
    @RequestMapping(value = "/updatePage", method = RequestMethod.GET)
    public String updateForm(Model model){

        model.addAttribute("formData",new FormData());
        return "updateForm";
    }

    //Operations on DataBase


    @RequestMapping(value="/insertData",method=RequestMethod.POST)
    public String formData(@ModelAttribute("formData")FormData formData,ModelMap model) 
    {
        formDataService.insertFormData(formData);
        return "redirect:/successReg";  
    }

    @RequestMapping(value="/loginAndGetData", method=RequestMethod.POST)
    public String loginData(Model model) 
    {
        model.addAttribute("formData", new FormData());
        model.addAttribute("listFormData", formDataService.listFormData());
        return "redirect:/result";

    }

    @RequestMapping(value="/updateData",method=RequestMethod.POST)
    public ModelAndView updateForm(@ModelAttribute("formData")FormData formData)
    {
        formDataService.updateFormData(formData);
        return new ModelAndView("updateSuccess");
    }


    //for dropdown country list
    @ModelAttribute("countryList")
    public List<String> getcountryList(){
        List<String> countryList=new ArrayList<String>();
        countryList.add("India");
        countryList.add("USA");
        countryList.add("Canada");

        return countryList;

    }

    //for dropdown idCard list
    @ModelAttribute("idList")
    public List<String> getidList(){

        List<String> idList=new ArrayList<String>();
        idList.add("Adhaar");
        idList.add("Driving Licence");
        idList.add("Pancard");
        idList.add("VoterCard");

        return idList;
    }

    //for dropdown Address list
    @ModelAttribute("addList")
    public List<String> getaddList(){

        List<String> addList=new ArrayList<String>();
        addList.add("Passport");
        addList.add("RationCard");
        addList.add("Bank PassBook");
        addList.add("Utility_Bills");

        return addList;
    }



    @RequestMapping(value="/img",method=RequestMethod.GET)
    @ResponseBody
    public void download(@RequestParam("filename") String filename,HttpServletResponse response) throws IOException{


        PrintWriter out = response.getWriter();

        String path = "C:/Users/Public/Pictures/Sample Pictures/";

        response.setHeader("Content-Disposition", "attachment; filename=\""+ filename + "\"");

        FileInputStream fileInputStream = new FileInputStream(path+filename);

        int i;
        while ((i = fileInputStream.read()) != -1)
        {
            out.write(i);
        }

        fileInputStream.close();
        out.close();

    }
}

数据服务类: FormData

package com.spring.services;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="mdms.mdm_registration_form")
public class FormData 
{
    @Column(name="Fname")
    private String Fname;

    @Column(name="Lname")
    private String Lname;

    @Column(name="mobile")
    private String mobile;

    @Column(name="gend")
    private String gend;

    @Column(name="marry")
    private String marry;

    @Column(name="dob")
    private Date dob;

    @Id
    @Column(name="email")
    private String email;

    @Column(name="password")
    private String password;

    @Column(name="address")
    private String address;

    @Column(name="country")
    private String country;

    @Column(name="idcard")
    private String idcard;

    @Column(name="idupload")
    private String idupload;

    @Column(name="addid")
    private String addid;

    @Column(name="adupload")
    private String adupload;



    public FormData(){

    }

    public String getFname() {
        return Fname;
    }
    public void setFname(String fname) {
        Fname = fname;
    }

    public String getLname() {
        return Lname;
    }

    public void setLname(String lname) {
        Lname = lname;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getGend() {
        return gend;
    }

    public void setGend(String gend) {
        this.gend = gend;
    }

    public String getMarry() {
        return marry;
    }

    public void setMarry(String marry) {
        this.marry = marry;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getIdcard() {
        return idcard;
    }

    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }

    public String getAddid() {
        return addid;
    }

    public void setAddid(String addid) {
        this.addid = addid;
    }

    public String getIdupload() {
        return idupload;
    }

    public void setIdupload(String idupload) {
        this.idupload = idupload;
    }

    public String getAdupload() {
        return adupload;
    }

    public void setAdupload(String adupload) {
        this.adupload = adupload;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

FormDataService

package com.spring.services;

import java.util.List;

public interface FormDataService {

    public void insertFormData(FormData formData);

    public FormData getFormDataByEmail(String email);

    public void updateFormData(FormData formData);

    public List<FormData> listFormData();


}

FormDataServiceImpl

package com.spring.services;

import java.util.List;

import javax.transaction.Transactional;

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

import com.spring.dao.FormDao;


@Service
public class FormDataServiceImpl implements FormDataService
{
    @Autowired
    private FormDao formDao;

    @Override
    @Transactional
    public void insertFormData(FormData formData)
    {
        formDao.insertFormData(formData);
    }

    @Override
    @Transactional
    public FormData getFormDataByEmail(String email)
    {

        return formDao.getFormDataByEmail(email);
    }

    @Override
    @Transactional
    public void updateFormData(FormData formData)
    {
        formDao.updateFormData(formData);
    }

    @Override
    @Transactional
    public List<FormData> listFormData()
    {
        return this.formDao.listFormData();
    }

}

数据访问层类 FormDao:

package com.spring.dao;

import java.util.List;

import org.springframework.stereotype.Service;

import com.spring.services.FormData;


public interface FormDao {

    public void insertFormData(FormData formData);

    public FormData getFormDataByEmail(String email);

    public void updateFormData(FormData formData);

    public List<FormData> listFormData();

}

FormDaoImpl:

package com.spring.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.spring.services.FormData;


@Repository

public class FormDaoImpl implements FormDao
{

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void insertFormData(FormData formData) 
    {
        Session session = sessionFactory.getCurrentSession();
        session.persist(formData);

    }

    @Override
    public FormData getFormDataByEmail(String email) {

        Session session = sessionFactory.getCurrentSession();
        FormData formData = (FormData)session.get(FormData.class,new String (email));

        return formData;
    }

    @Override
    public void updateFormData(FormData formData)
    {
        Session session = sessionFactory.getCurrentSession();
        session.update(formData);

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<FormData> listFormData() 
    {

        Session session = sessionFactory.getCurrentSession();
        List<FormData> listFormData = session.createQuery("form FormData").list();
        return null;
    }


}

Dispacher-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:p="http://www.springframework.org/schema/p"
 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
   http://www.springframework.org/schema/jdbc
   http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">


    <context:annotation-config />

    <context:component-scan base-package = "com.spring" />

    <mvc:annotation-driven />

    <bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

             <!--   DataBase Configuration -->

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url" value="jdbc:sqlserver://10.10.39.119:1433;databaseName=UGVCL_PREPOD" />
        <property name="username" value="sa" />
        <property name="password" value="Myroot@999" />
    </bean>

    <!-- Session Factory configuration -->

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.spring.services.FormData" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>

                <!-- Connection Pool Configuration -->

                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size" >20</prop>
                <prop key="hibernate.c3p0.timeout">1800</prop>
                <prop key="hibernate.c3p0.max_statements" >150</prop>
                <prop key="hibernate.c3p0.idle_test_period" >300</prop>
                <prop key="hibernate.c3p0.acquire_increment" >1</prop>
            </props>
        </property>
    </bean>

    <!-- Transaction Configuration -->

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory">
    </bean>

    <tx:annotation-driven/>    
</beans>

web.xml

<?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>SpringForms</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>

请帮助。

1 个答案:

答案 0 :(得分:0)

您在这里使用spring + hibernate,对吗?

  

解决您的问题

那您为什么要使用Spring JDBC <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >

这就是为什么您获得org.springframework.beans.factory.BeanCreationException的原因。和Unsatisfied dependency expressed through field 'sessionFactory';

  

解决问题的方法

首先,您需要在pom.xml中导入Hibernate-C3P0

    <!-- Hibernate-C3P0 Integration -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

然后针对您的休眠配置在Dispatcher-servlet.xml中执行以下步骤。 (您的spring-MVC配置可以)

<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
              destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo?useSSL=false&amp;serverTimezone=UTC" />
    <property name="user" value="root" />
    <property name="password" value="root" />
    <!-- these are connection pool properties for C3P0 -->
    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="20" />
    <property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="packagesToScan" value="net.javaguides.springmvc.entity" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />
<!-- Add support for reading web resources: css, images, js, etc ... -->
<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>

这只是一个sample code,您可以输入自己的值和自己的配置...祝您编程愉快!