标签:接口 信息 null hibernate unknown entity
这是因为Hibernate 4.x和5.x中的ServiceRegistry写法不同造成的
Hibernate 4.x
//1.创建一个SessionFactory对象
SessionFactory sessionFactory=null;
//2.创建一个configuration对象,必须要根据你基本配置信息和映射关系
Configuration config=new Configuration().configure();
//3.要创建session对象
Session session=null;
//4.把注册信息传入serviceregistry接口中,Hibernate 4.X的做法
ServiceRegistry bulidServiceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//.applySettings(config.getProperties()).build();
sessionFactory = config.buildSessionFactory(bulidServiceRegistry);
//5.获取session
session = sessionFactory.openSession();
而Hibernate 5.x没有ServiceRegistryBuilder()方法,而是替换成了StandardServiceRegistryBuilder()方法
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
但是此种方法获取不到实体类 所以可以直接写成
SessionFactory sessionFactory = null;
Configuration config = new Configuration().configure();
Session session =null ;
//ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
sessionFactory =config.buildSessionFactory();
session = sessionFactory.openSession();
本文出自 “12901799” 博客,请务必保留此出处http://12911799.blog.51cto.com/12901799/1927538
Hibernate报错org.hibernate.MappingException: Unknown entity: 。。。。
标签:接口 信息 null hibernate unknown entity
原文地址:http://12911799.blog.51cto.com/12901799/1927538