Home > Java > javaTutorial > Why am I Getting \'org.hibernate.MappingException: Unknown Entity\' in Hibernate 5 with MySQL?

Why am I Getting \'org.hibernate.MappingException: Unknown Entity\' in Hibernate 5 with MySQL?

Mary-Kate Olsen
Release: 2024-10-29 16:35:02
Original
1102 people have browsed it

Why am I Getting

Hibernate 5: Understanding "org.hibernate.MappingException: Unknown Entity"

When integrating Hibernate 5.0 with MySQL, developers may encounter the error message "org.hibernate.MappingException: Unknown entity." This issue arises in Hibernate 5.0.0 and 5.0.1 but not in Hibernate 4.3.9.

Problem Analysis

To resolve this error, it's essential to understand why it occurs. In Hibernate 5, unlike previous versions, the default configuration process does not automatically load entity mappings. This means that when configuration.buildSessionFactory(sr) is called, it lacks information about mapped entities.

Incorrect Hibernate 5 Tutorial:

The Hibernate 5 tutorial provides an incorrect code sample that leads to this error:

return new Configuration().configure().buildSessionFactory(
                new StandardServiceRegistryBuilder().build() );
Copy after login

This code doesn't properly configure the entity mappings.

Solution: Loading Entity Mappings

To fix the issue, you can load entity mappings correctly using one of the following methods:

  1. Standard Configuration Files: Use the simplified approach if you have standard configuration files hibernate.cfg.xml and hibernate.properties:

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Copy after login
  2. Load Properties: For other property files, use a StandardServiceRegistryBuilder to load properties:

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

    This requires the hibernate-h2.properties file to be in the classpath.

  3. Load Properties from Path: Use this method to load properties from a specific file path:

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

Conclusion

By using one of these solutions to load entity mappings, you can resolve the "Unknown Entity" error when integrating Hibernate 5.0 with MySQL. Remember that the incorrect code sample in the Hibernate 5 tutorial should be avoided.

The above is the detailed content of Why am I Getting \'org.hibernate.MappingException: Unknown Entity\' in Hibernate 5 with MySQL?. For more information, please follow other related articles on the PHP Chinese website!

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