如何解决“org.hibernate.MappingException:未知实体”错误?

时间:2015-04-22 10:11:58

标签: java hibernate

我在Exception.am下面使用 Struts2 Hibernate 来开发web应用程序。任何人都可以帮我解决这个问题吗?

  

org.hibernate.MappingException:未知实体:Pojo.USER [Packagename.entityClassname]

Hibernate.cfg.xml文件内容: -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/databasename
</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="cache.provider_class">
org.hibernate.cache.HashtableCacheProvider</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
</session-factory>
</hibernate-configuration>

USER的实体类内容: -

package Pojo;

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

@Entity
@Table(name="user")
public class USER {

    @Id
    @GeneratedValue
    @Column(name="USER_ID")
    private int id;

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

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

    @Column(name="USER_GENDER")
    private String gender;

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

    @Column(name="USER_ABOUT_YOU")
    private String aboutyou;

    @Column(name="USER_MAILING_LIST")
    private boolean mailinglist;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getCountry() {
        return country;
    }

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

    public String getAboutyou() {
        return aboutyou;
    }

    public void setAboutyou(String aboutyou) {
        this.aboutyou = aboutyou;
    }

    public boolean isMailinglist() {
        return mailinglist;
    }

    public void setMailinglist(boolean mailinglist) {
        this.mailinglist = mailinglist;
    }

}

2 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/databasename
</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="cache.provider_class">
org.hibernate.cache.HashtableCacheProvider</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<mapping class="Pojo.User">
</session-factory>

</hibernate-configuration>

使用映射类,以便hibernate知道你的实体类 - &gt; <mapping class="Pojo.User">

答案 1 :(得分:1)

您的Hibernate会话不知道

USER实体。在<mapping class="Pojo.USER"/>配置中添加session-factory

    ...
    <property name="current_session_context_class">thread</property>
    <property name="hibernate.show_sql">false</property>
    <mapping class="Pojo.USER"/>
</session-factory>
</hibernate-configuration>