Hibernate Maven MappingException未知实体

问题描述:

在Maven中使用休眠模式时出现异常.休眠版本为5.1.0.Final. 例外是: 这是我的项目结构:

I got an exception when I use hibernate in Maven. The hibernate version is 5.1.0.Final. The exception is: Here is my project structure:

这是我的Entity类ABC:

Here is my Entity class ABC:

package com;
import javax.persistence.*;
@Entity

@Table(name = "abc_inf")

public class ABC {

    @Id@GeneratedValue
    private Integer id;

    private String name;

    public ABC() {
    }

    setters and getters omitted 

}

这是我的主班:

package com;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Main {
    public static void main(String[] args) {
        Configuration conf = new Configuration().configure();
        ServiceRegistry sr = new             StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
        SessionFactory sf = conf.buildSessionFactory(sr);
        Session session = sf.openSession();

        ABC abc = new ABC();
        abc.setName("abc");

        session.save(abc);
        session.flush();
        session.close();
        sf.close();
    }
}

这是我的hibernate.cfg.xml:

Here is my hibernate.cfg.xml:

<hibernate-configuration>
  <session-factory>
    mysql connection and properties settings omitted
    <mapping class="com.ABC"/>
  </session-factory>
</hibernate-configuration>

这是Hibernate 5的配置问题.您不能使用此代码来建立会话工厂

It is a Hibernate 5 configuration problem. You can't use this code to build a session factory

Configuration conf = new Configuration().configure();
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(sr);

改为使用此

SessionFactory sf = new Configuration().configure().buildSessionFactory();

请参阅其他注释

休眠5:-org.hibernate.MappingException:未知实体