2016-10-28 98 views
0

我得到異常 org.hibernate.MappingException:未知實體com.pro.entity.Userorg.hibernate.MappingException:未知實體hibernate4 spring4

我已經看到了類似的帖子,但幾乎所有的人提供瞭解決方案將@Entity註釋更改爲javax.persistence,而不是hibernate。從我開始使用正確的註釋以來,這在我的情況下並不有用。以下是實體類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; 
    } 

    } 

DAO類代碼:

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> 
+1

除非你有一個祕密Hibernate配置的地方,Hibernate知道也不關心你的實體......您還沒有指定一個包手動掃描或註冊實體。沒有這一點,對於休眠你的實體根本不存在,無論它的註釋。 –

+0

你把User.java放在哪裏?它在''com.pro.entity''包裏面嗎? –

+0

在發佈之前,我實際上已經更改了代碼,但相信我的項目結構是按照彈簧規格。 – sbajpai

回答

0

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

它應該工作,然後

0

謝謝你們。我得到了解決....只是需要加入我的context.xml文件下列

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