Hibernate不希望在hibernate.cfg.xml中映射该类

时间:2016-07-03 09:47:11

标签: java hibernate persistence

我有一个类用户是实体。当我试图在hibernate.cfg.xml中映射它时

<mapping class="com.igorgorbunov3333.entities.User"/> 

收到错误:

  

线程“main”中的异常org.hibernate.MappingException:未知   entity:com.igorgorbunov3333.entities.User

但是当我映射类用户programmaticaly它工作正常。

configuration.addAnnotatedClass(User.class);

是什么原因?

我的实体课程:

package com.igorgorbunov3333.entities;

import javax.persistence.*;


/**
 * Created by Игорь on 01.07.2016.
 */

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

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;

    private String email;

    private String password;

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

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

3 个答案:

答案 0 :(得分:0)

你的hibernate配置文件(hibernate.cfg.xml)应该包含一个到实体类的映射。这样的东西:

<mapping resource="com/igorgorbunov3333/entities/User.hbm.xml"/>

请你检查一下。

答案 1 :(得分:0)

尝试使用4.3.6.Final版本的hibernate。您使用什么hibernate版本?用5.2.1.Final我看到了同样的问题。通过改变hibernate版本来解决。

答案  https://forum.hibernate.org/viewtopic.php?f=1&t=1043461&p=2489997&hilit=hibernate.cfg.xml#p2489997

答案 2 :(得分:0)

我有同样的错误,我做了同样的事情(直接映射了类),它也有效。它对我没有任何意义所以我查看了源代码并看到了这个

/**
 * Use the mappings and properties specified in the given application resource. The format of the resource is
 * defined in <tt>hibernate-configuration-3.0.dtd</tt>.
 *
 * @param resource The resource to use
 *
 * @return this for method chaining
 *
 * @throws HibernateException Generally indicates we cannot find the named resource
 */
public Configuration configure(String resource) throws HibernateException {
    standardServiceRegistryBuilder.configure( resource );
    // todo : still need to have StandardServiceRegistryBuilder handle the "other cfg.xml" elements.
    //      currently it just reads the config properties
    properties.putAll( standardServiceRegistryBuilder.getSettings() );
    return this;
}

基本上,并非当前正在读取hibernate.cfg.xml文件中的所有项目,而且我无法找出hibernate API提供的任何替代方法