Home > Database > Mysql Tutorial > Hibernate 5: How to Solve 'org.hibernate.MappingException: Unknown entity'?

Hibernate 5: How to Solve 'org.hibernate.MappingException: Unknown entity'?

Patricia Arquette
Release: 2024-12-23 10:04:43
Original
816 people have browsed it

Hibernate 5: How to Solve

Hibernate 5: Resolving "org.hibernate.MappingException: Unknown entity" Issue

The "org.hibernate.MappingException: Unknown entity" error commonly encountered when integrating Hibernate 5 with database systems stems from a configuration issue. This occurs specifically for Hibernate versions 5.0.0 and 5.0.1.

Configuration Flaw

The problem lies within the sessionFactory creation process. The following code snippet from the sample code provided illustrates the issue:

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

When attempting to build the session factory using the buildSessionFactory method while passing in the ServiceRegistry, Hibernate 5 loses track of the mapping information previously loaded via the configure method.

Solution

To rectify this issue, alternative approaches for creating the session factory can be employed, depending on the configuration being used.

Loading Properties

For standard configuration files (hibernate.cfg.xml and hibernate.properties), the session factory can be created without utilizing the ServiceRegistry as seen below:

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

Alternatively, if properties are stored in a file other than hibernate.properties, they can be loaded using the StandardServiceRegistryBuilder:

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

Similarly, properties can also be loaded from a specific 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);
Copy after login

The above is the detailed content of Hibernate 5: How to Solve 'org.hibernate.MappingException: Unknown entity'?. 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