
org.hibernate.MappingException: Unknown Entity in Hibernate 5
Problem: An exception occurs with the message "org.hibernate.MappingException: Unknown entity" when attempting to integrate Hibernate 5.0 with MySQL.
Cause: This issue is encountered specifically with Hibernate 5.0.0 and 5.0.1 versions but not with Hibernate 4.3.9. The error stems from a discrepancy in how Hibernate 5 handles configuration compared to previous versions.
Solution: To resolve this issue, adjust the code responsible for creating the SessionFactory:
1 2 3 4 5 6 7 | Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sf = configuration.buildSessionFactory(sr);
|
Copy after login
Correct approach for Hibernate 5:
-
Standard XML Configuration Files (hibernate.cfg.xml and hibernate.properties):
1 | SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
|
Copy after login
-
Loading Properties from a non-standard file:
1 2 | ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().loadProperties( "hibernate-h2.properties" ).build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);
|
Copy after login
-
Loading Properties from File Path:
1 2 3 | File propertiesPath = new File( "some_path" );
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().loadProperties(propertiesPath).build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);
|
Copy after login
The above is the detailed content of How to Resolve 'org.hibernate.MappingException: Unknown Entity' in Hibernate 5?. For more information, please follow other related articles on the PHP Chinese website!