Hibernate 5;org.hibernate.MappingException: Unknown entity: com.my.model.User
来源:互联网 发布:windows高级选项菜单 编辑:程序博客网 时间:2024/06/07 01:47
I have fixed the same issue with Hibernate 5. There is a problem in this code
Configuration configuration = new Configuration();configuration.configure();ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings( configuration.getProperties()).build();SessionFactory sf = configuration.buildSessionFactory(sr);
This code works fine for Hibernate 4.3.5, but the same code has the same issue for Hibernate 5.
When you do configuration.buildSessionFactory(sr), using Hibernate 5, Configuration losts all information about mapping that gets by call configuration.configure().
Solution
To fix the issue, if you use standard configuration files hibernate.cfg.xml and hibernate.properties, you can create the session factory by this way (without ServiceRegistry)
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Loading properties
If you have properties in a file other then hibernate.properties, you can build session factory using StandardServiceRegistryBuilder (anyway, if you have hibernate.properties and other file, it will be loaded both)
To load properties as a resource
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(). configure().loadProperties("hibernate-h2.properties").build();SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);
You need to have hibernate-h2.properties in the class path (root of the sources folder, resources folder). You can specify a path from the root source folder too /com/github/xxx/model/hibernate-h2.properties.
To load properties from a path in the file system
File propertiesPath = new File("some_path");ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(). configure().loadProperties(propertiesPath).build();SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);
You can find an example console application using this approach here fluent-hibernate-mysql. It uses a utility class to build the session factory from the fluent-hibernate library.
Incorrect Hibernate 5 tutorial
There is an incorrect example in Hibernate 5 tutorial 1.1.6. Startup and helpers. It uses this code
return new Configuration().configure().buildSessionFactory( new StandardServiceRegistryBuilder().build() );
It doesn’t do a proper configuration.
url:https://stackoverflow.com/questions/32405031/hibernate-5-org-hibernate-mappingexception-unknown-entity
- Hibernate 5;org.hibernate.MappingException: Unknown entity: com.my.model.User
- org.hibernate.MappingException:unknown entity:com.lmy.model.User
- org.hibernate.MappingException: Unknown entity: com.pkms.entity.User
- org.hibernate.MappingException: Unknown entity: com.test.xbt.entity.User
- org.hibernate.MappingException: Unknown entity: com.zhou.web.entity.User
- org.hibernate.MappingException: Unknown entity: com.fei.model.Student
- org.hibernate.MappingException: Unknown entity: dxm.com.vo.User
- org.hibernate.MappingException: Unknown entity: com.hibernate.model.Teacher at org.hibernate.impl.S
- Unknown entity异常:org.hibernate.MappingException: Unknown entity:User findById
- Hibernate 5 :- org.hibernate.MappingException: Unknown entity
- (注解方式持久化类报错解决方案-仅供参考)org.hibernate.MappingException: Unknown entity: com.rx.entity.po.User
- Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.hibernate.domain.User
- 求助!第一个hibernate程序报错 org.hibernate.MappingException: Unknown entity: com.pojo.User
- 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
- Maven笔记
- python如何调用c语言
- jQuery多图片上传同时带预览
- 51nod 1179 最大的公约数
- 51Nod(1627)
- Hibernate 5;org.hibernate.MappingException: Unknown entity: com.my.model.User
- django基础知识
- SVM分类器的实现(包括交叉验证选择参数,Dlib,可视化)
- 【机器学习入门】Andrew NG《Machine Learning》课程笔记之二 :基本概念、代价函数、梯度下降和线性回归
- C++退出函数exit
- USACO-2008-Cow Contest
- 最大子序列数,只测试了几组数据,求大佬指点
- 【SpringBoot】1.SpringBoot介绍
- 【java.lang.UnsupportedClassVersionError】版本不一致出错