【问题标题】:org.hibernate.MappingException: Unknown entity hibernate4 spring4org.hibernate.MappingException:未知实体hibernate4 spring4
【发布时间】:2017-03-11 02:18:53
【问题描述】:

我遇到了异常 org.hibernate.MappingException: 未知实体 com.pro.entity.User

我看过类似的帖子,但几乎所有帖子都提供了将 @Entity 注释更改为 javax.persistence 而不是休眠的解决方案。在我的情况下,这没有用,因为我从一开始就使用正确的注释。这是实体类 dao 和 servlet 上下文的代码。

package com.pro.entity;

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="user")
public class User {

    private static final long serialVersionUID = 1L;


    @Id
    @Column(name="userid")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private String id;

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

    public String getId() {
        return id;
    }

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

    public String getPassword() {
        return password;
    }

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

    }

道类代码:

public User getUser(String userid)
{

        Session session = this.sessionFactory.getCurrentSession();

        User user = (User)session.load(User.class, userid);

        return user;

    }

context.xml

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

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


    <beans:bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url"
            value="jdbc:mysql://localhost:3306/pro?useSSL=false" />
        <beans:property name="username" value="user" />
        <beans:property name="password" value="password" />
    </beans:bean>

    <beans:bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"/>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">${hibernate.dialect}</beans:prop>
                <beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>


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

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

    <beans:bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"></beans:bean>



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

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <mvc:annotation-driven />

</beans:beans>

【问题讨论】:

  • 除非你在某个地方有一个秘密的休眠配置,否则休眠对你的实体一无所知......你没有指定一个包来扫描或手动注册实体。没有它,对于休眠你的实体根本不存在,不管它上面的注释。
  • 你把 User.java 放在哪里了?它在com.pro.entity 包内吗?
  • 在发帖之前我实际上已经更改了一个代码,但相信我,项目结构符合 spring 规范。

标签: java spring-4 hibernate-4.x


【解决方案1】:

更新&lt;context:component-scan base-package="com.pro.controller" /&gt;&lt;context:component-scan base-package="com.pro" /&gt; 在 context.xml 中

它应该可以工作

【讨论】:

    【解决方案2】:

    谢谢你们。我得到了解决方案....只需要在我的 context.xml 文件中添加以下内容

    <beans:property name="annotatedClasses">
                <beans:list>
                    <beans:value>com.pro.User</beans:value>
                </beans:list>
            </beans:property>
    

    【讨论】:

      猜你喜欢
      • 2012-12-04
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 2011-11-28
      • 2014-04-16
      • 2014-08-05
      • 2012-06-17
      相关资源
      最近更新 更多