关于Hibernate 5 和 Hibernate 4 在创建SessionFactory的不同点分析(解决 org.hibernate.MappingException: Unknown entit
来源:互联网 发布:淘宝怎么看年销售额 编辑:程序博客网 时间:2024/06/14 22:22
参考:http://www.cnblogs.com/wangxiaoha/p/6231888.html
关于Hibernate 5 和 Hibernate 4 在创建SessionFactory的不同点分析(解决 org.hibernate.MappingException: Unknown entity: xx类报错问题)
Hibernate4版本的SessionFactory实例构建的步骤是这样的(也是很多学习资料的通用范本):
//Configuration就是代表着hibernate的那个xml配置文件对象,如果configure方法中没有参数的话,默认是就是hibernate.cfg.xml。
Configuration conf = new Configuration().configure();
//选择Hibernate.cfg.xml
Configuration conf = new Configuration().configure(“/Hibernate.cfg.xml”);
//服务注册,这是使用创建者模式,根据配置文件中的配置字段来构建注册服务(这应该是hibernate架构中注册服务的通用流程)。
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
applySettings(conf.getProperties()).build();
//使用实例化好了的注册服务,使用Configuration中的工厂模式实例化了SessionFactory
SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
如果你用的是Hibernate4的版本,这样做完全OK的,运行的时候不会报MappingException。
但是如果你使用Hibernate5的版本,就会报错。那么Hibernate5应该怎样构建SessionFactory呢,如下:
//和V4版本比,V5版本看不到configure对象了。直接使用创建者模式构建出了标准服务注册对象
//1. 配置类型安全的准服务注册类,这是当前应用的单例对象,不作修改,所以声明为final
//在configure(“cfg/hibernate.cfg.xml”)方法中,如果不指定资源路径,默认在类路径下寻找名为hibernate.cfg.xml的文件
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure(“cfg/hibernate.cfg.xml”).build();
//2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
- 关于Hibernate 5 和 Hibernate 4 在创建SessionFactory的不同点分析(解决 org.hibernate.MappingException: Unknown entit
- 在版本问题上解决 org.hibernate.MappingException: Unknown entit xxx
- Hibernate 5 :- org.hibernate.MappingException: Unknown entity
- “org.hibernate.MappingException: Unknown entity” 异常解决
- org.hibernate.MappingException: Unknown entity解决
- 关于org.hibernate.MappingException: Unknown entity:错误
- Rhyme/ Hibernate5.x SessionFactory创建方式 以及org.hibernate.MappingException: Unknown entity异常
- org.hibernate.MappingException: Unknown entity:的解决办法
- org.hibernate.MappingException: Unknown entity
- org.hibernate.MappingException: Unknown entity
- org.hibernate.MappingException: Unknown entity
- org.hibernate.MappingException: Unknown entity:
- org.hibernate.MappingException: Unknown entity
- org.hibernate.MappingException: Unknown entity:
- org.hibernate.MappingException: Unknown entity:
- org.hibernate.MappingException: Unknown entity:
- org.hibernate.MappingException: Unknown entity:
- org.hibernate.MappingException: Unknown entity
- Ajax 入门之 GET 与 POST 的不同 (2)
- 二叉树的深度
- 最短子数组练习题
- CSS的float属性对周围元素位置的影响
- Godfather POJ - 3107
- 关于Hibernate 5 和 Hibernate 4 在创建SessionFactory的不同点分析(解决 org.hibernate.MappingException: Unknown entit
- python全栈1-Html基础
- 如何计算时间复杂度
- 相邻两数最大差值练习题
- 爬虫文章索引
- android与H5互相调用
- 快速排序
- Linux——ls命令
- 详细讨论OpenCV中的数据结构