在产品java类中输入的数据给出了异常

时间:2016-09-11 14:02:45

标签: java spring hibernate spring-mvc javabeans

您好我在springMVC应用程序代码中遇到以下异常。我读了一些上一个问题,但没有找到任何帮助。

我在申请中有两个实体,即

  • 用户
  • 产品

在产品java类中输入的数据给出了异常: -

  

例外   org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.hibernate.MappingException:未知实体:com.onlineshopping.model.Product       org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)       org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)       javax.servlet.http.HttpServlet.service(HttpServlet.java:641)       javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Product.java类是:

package com.onlineshopping.model;

import java.io.Serializable;

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

@Entity
@Table(name="Product")
public class Product implements Serializable{
    private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="productid")
        private Integer proid;

        @Column(name="productname")
        private String proname;


        @Column(name="price")
        private Integer price;


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

        public Integer getProid() {
            return proid;
        }

        public void setProid(Integer proid) {
            this.proid = proid;
        }

        public String getProname() {
            return proname;
        }

        public void setProname(String proname) {
            this.proname = proname;
        }

        public Integer getPrice() {
            return price;
        }

        public void setPrice(Integer price) {
            this.price = price;
        }

        public String getDetails() {
            return details;
        }

        public void setDetails(String details) {
            this.details = details;
        }
    }

配置文件:

<?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: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/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-3.0.xsd">

    <context:property-placeholder location="classpath:resources/database.properties" />
    <context:component-scan base-package="com.onlineshopping" />

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

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

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.onlineshopping.model.Customer</value>
            </list>
        </property> 
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
            </props>
        </property>
    </bean>

    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

有人可以告诉我为什么会发生此异常。其他用户实体正常运行。 感谢

1 个答案:

答案 0 :(得分:1)

所以评论后@dhs找到答案

会话工厂需要setPackagesToScan,所以请尝试将此添加到annotatedClasses中 的 com.onlineshopping.model.Product

> [ [x+y+z] | x <- [1,11], y <- [1,11], z <- [1,11]]
> [[3],[13],[13],[23],[13],[23],[23],[33]]