Hibernate -Unknown实体中的映射异常

时间:2016-09-01 02:57:15

标签: java hibernate intellij-idea

我只是编写简单的hibernate演示程序。但我得到了Mapping异常。

hibernate.cfg.xml位于src目录下

<?xml version='1.0' encoding='utf-8'?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="hibernateProject.com.himal.hibernate.HibernateTest"/>

    </session-factory>

</hibernate-configuration>

在src / hibernateProject.com.himal.hibernatePractise.dto下,我创建了UserDetails类。

package hibernateProject.com.himal.hibernatePractise.dto;

import javax.persistence.Entity;
import javax.persistence.Id;

/**
 * Created by Himal Acharya on 2016-09-01.
 */

@Entity
public class UserDetails {

    @Id
    //for primary key
    private int userId;
    private String userName;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

我的Test课程是

package hibernateProject.com.himal.hibernate;

import hibernateProject.com.himal.hibernatePractise.dto.UserDetails;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


/**
 * Created by Himal Acharya on 2016-09-01.
 */
public class HibernateTest {

    public static void main(String[] args) {
        //Initiate object of userDetails
        UserDetails user=new UserDetails();

        //setting value
        user.setUserId(1);
        user.setUserName("Himal");

        //get the configuration

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        session.save(user);

        //Ending Transcation
        session.getTransaction().commit();



    }
}

但我收到了以下错误:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: hibernateProject.com.himal.hibernatePractise.dto.UserDetails 

错误发生在hibernateProject.com.himal.hibernate.HibernateTest.main中,如cosole in line session.save(user)中所述;

2 个答案:

答案 0 :(得分:1)

在你的hibernate.cfg.xml中,你必须映射实体:

<mapping class="hibernateProject.com.himal.hibernatePractise.dto.UserDetails "/>

答案 1 :(得分:0)

您正在使用JPA实体而不是休眠@org.hibernate.annotations.Entity所以请替换此

import javax.persistence.Entity;
import javax.persistence.Id;

位于JPA

之下

由此

import org.hibernate.*;

并在你的hibernate-config.xml

而不是映射您的测试类通过添加

来映射您的实体类
<mapping class="hibernateProject.com.himal.hibernatePractise.dto.User‌​Details"/>

遵循com.himal.hibernatePractise.dto.User‌​‌​Details

等包的命名惯例