Hibernate5 Unknown entity错误解决方法

时间:2021-09-20 10:49:01

报错如下
Exception in thread “main” org.hibernate.MappingException: Unknown entity:

这有几种情况,一种是没有找到hibernate.cfg.xml
可以看看是不是在cfg文件中少加了

<mapping resource="po/Student.hbm.xml" /

或者拼写错误之类的…

另一种是声明SessionFactory出现问题,不同版本的声明方式不同
Hibernate3

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

Hibernate4

Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

Hebernate5之后改成了这样

StandardServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().configure().build();
SessionFactory sessionFactory=new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();