Home > Database > Mysql Tutorial > Hibernate 5 'Unknown Entity' Error: How to Properly Configure SessionFactory for Entity Mapping?

Hibernate 5 'Unknown Entity' Error: How to Properly Configure SessionFactory for Entity Mapping?

Linda Hamilton
Release: 2024-12-15 03:45:23
Original
439 people have browsed it

Hibernate 5

Hibernate 5: Resolving "Unknown Entity" Error During Integration

While attempting to integrate Hibernate 5.0 with MySQL, many developers encounter the error message "org.hibernate.MappingException: Unknown entity." This issue arises specifically with Hibernate versions 5.0.0 and 5.0.1, while previous versions such as 4.3.9 function correctly.

Problem Definition

The "Unknown entity" error occurs when Hibernate is unable to recognize the class annotated as an entity. This can happen if the mapping metadata for the class is not properly configured or added to the Hibernate configuration.

Configuration Details

The provided Maven dependencies and hibernate.cfg.xml configuration seem to be correct. The User class is also annotated as an entity and has been mapped to the database table "User_table."

Cause and Solution

The root cause of this issue lies in the process of creating the SessionFactory in the HibernateMain class. In Hibernate 5, the following code is used to configure and build the SessionFactory:

Configuration configuration = new Configuration();
configuration.configure();

ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(
    configuration.getProperties()).build();

SessionFactory sf = configuration.buildSessionFactory(sr);
Copy after login

The problem arises because using configuration.buildSessionFactory(sr) in Hibernate 5 causes the configuration to lose the mapping information obtained by calling configuration.configure(). As a result, Hibernate is unable to recognize the User class as an annotated entity.

Solution

To resolve this issue, there are two approaches:

Using Standard Configuration Files (hibernate.cfg.xml and hibernate.properties)

If you are using standard configuration files, you can create the SessionFactory without using ServiceRegistry.

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Copy after login

Loading Properties

If you have properties in a separate file, you can load them and build the SessionFactory using StandardServiceRegistryBuilder.

Loading Properties as a Resource

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
    configure().loadProperties("hibernate-h2.properties").build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);
Copy after login

Loading Properties from a Path

File propertiesPath = new File("some_path");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
    configure().loadProperties(propertiesPath).build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);
Copy after login

Additional Notes

  • The example tutorial in Hibernate 5 documentation (1.1.6 Startup and helpers) provides an incorrect example that doesn't perform proper configuration.
  • Using the incorrect code can lead to the "Unknown entity" error.

The above is the detailed content of Hibernate 5 'Unknown Entity' Error: How to Properly Configure SessionFactory for Entity Mapping?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template