org.springframework.orm.hibernate3.HibernateSystemException:未知实体

时间:2011-11-13 17:18:54

标签: java hibernate spring

这是一个使用IntelliJ的Spring mvc应用程序。

我收到了这个错误:

org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: com.testproj1.core.model.User; nested exception is org.hibernate.MappingException: Unknown entity: com.testproj1.core.model.User
    org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)
    org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
    org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512)
    org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506)
    com.testproj1.core.doa.UserDaoImpl.get(UserDaoImpl.java:17)
    com.testproj1.core.service.UserServiceImpl.getUser(UserServiceImpl.java:22)
    com.testproj1.web.controllers.HomeController.index(HomeController.java:19)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

org.hibernate.MappingException: Unknown entity: com.testproj1.core.model.User
    org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:701)
    org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92)
    org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
    org.hibernate.impl.SessionImpl.get(SessionImpl.java:997)
    org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
    org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:519)
    org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512)
    org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506)
    com.testproj1.core.doa.UserDaoImpl.get(UserDaoImpl.java:17)
    com.testproj1.core.service.UserServiceImpl.getUser(UserServiceImpl.java:22)
    com.testproj1.web.controllers.HomeController.index(HomeController.java:19)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
testproj1

这是我的设置,hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <mapping package="com.testproj1.core.model"/>

        <mapping class="com.testproj1.core.model.User"/>


    </session-factory>

</hibernate-configuration>

我的servlet-context.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Scans within the base package of the application for @Components to configure as beans -->
    <!-- @Controller, @Service, @Configuration, etc. -->
    <context:component-scan base-package="com.testproj1" />

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />



     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/testproj1"/>
        <property name="username" value="root"/>
        <property name="password" value="123"/>
        <property name="maxActive" value="100"/>
        <property name="maxIdle" value="30"/>
        <property name="maxWait" value="1000"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="removeAbandoned" value="true"/>
        <property name="removeAbandonedTimeout" value="60"/>
    </bean>


    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--<property name="packagesToScan" value="com.testproj1.core.model"/>-->
        <property name="configLocation" value="/WEB-INF/config/hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.query.substitutions=true 'Y', false 'N'
                hibernate.show_sql=true
            </value>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.testproj1.core.model.User</value>
            </list>
        </property>
    </bean>

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->


    <bean id="userDao" class="com.testproj1.core.doa.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

</beans>

我的控制器:

public class HomeController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index() {
        System.out.println("hello, world!");

        User user = userService.getUser(1);

        System.out.println("Username is: " + user.getUsername());

        return "WEB-INF/views/home.jsp";
    }
}

UserServiceImpl:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    public User getUser(int id) {
        return userDao.get(id);
    }

}

在UserDAOImpl:

public class UserDaoImpl extends GenericDaoImpl<User, Integer> implements UserDao {



    public User get(int id) {
        User user = super.getHibernateTemplate().get(User.class, id); 


        return user;
    }

    public void addUser(User user)
    {
        super.getHibernateTemplate().save(user);
    }

}

用户模型:

package com.testproj1.core.model;

import org.hibernate.annotations.Entity;


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

@Entity
@Table(name="users")
public class User {

    protected Integer id;
    protected String user_name;

    @Id
    public Integer getId() {
        return id;
    }

    @Column
    public String getUsername() {
        return user_name;
    }


    public void setId(Integer id) {
        this.id = id;
    }

    public void setUsername(String user_name) {
        this.user_name = user_name;
    }

}

现在很明显它出于某种原因没有拿起我的用户模型。

我感到困惑的是,我确实设置了它,所以spring会在component-scan标签的servlet-context.xml中自动扫描jar(至少我认为)。

我在sessionFactory bean标记中也有annotatedClasses属性。

我在sessionFactory bean标签中注释掉了packagesToScan,因为我读到这个错误可能是因为循环引用,我认为这意味着它连接多次了?

2 个答案:

答案 0 :(得分:4)

使用javax.persistence.Entity而不是org.hibernate.annotations.Entity

答案 1 :(得分:0)

同样使用“javax.persistence.Id”@Id作为您的类标识符,即主键。