javamysqlhibernatejpa

org.hibernate.MappingException: Unknown entity: org.example.Person


I am trying to save a record of Person class into mySql database using pure java and hibernate but I get this error. these are my classes. I appreciate any help why this error is raised. With these classes all i get is : org.hibernate.MappingException: Unknown entity: org.example.Person .

Person:


    package org.example;
    
    import lombok.Data;
    
    @Data
    public class Person {
        private int id;
        private String firstName;
        private String lastName;
        private int age;
    
        public Person(int id, String firstName, String lastName, int age) {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
        }
    }

Person.hbm.xml:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name = "org.example.Person" table = "person">
        
        <id name = "id" type = "int" column = "id">
            <generator class="native"/>
        </id>

        <property name = "firstName" column = "firstname" type = "string"/>
        <property name = "lastName" column = "lastname" type = "string"/>
        <property name = "age" column = "age" type = "int"/>

    </class>
</hibernate-mapping>

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Version 8 MySQL hiberante-cfg.xml example for Hibernate 5 -->
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hbm</property>
        <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>
        <property name="connection.pool_size">3</property>
        <!--property name="dialect">org.hibernate.dialect.MySQLDialect</property-->
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
<!--         <mapping resource="Person.hbm.xml" />-->
        <mapping class="org.example.Person"/>
    </session-factory>
</hibernate-configuration>

HibernateUtil.java:


    package org.example;
    
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtil {
        private static SessionFactory sessionFactory = null;
    
        static {
            Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                    .applySettings(cfg.getProperties());
            sessionFactory = cfg.buildSessionFactory(builder.build());
        }
    
        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    }

and Main:


    package org.example;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    
    
    public class Main {
        public static void main(String[] args) {
            SessionFactory factory = HibernateUtil.getSessionFactory();
            Session session = factory.openSession();
            Transaction tx = session.getTransaction();
            try{
                tx.begin();
                Person p = new Person(1,"John","Doe",23);
                session.save(p);
                tx.commit();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                session.close();
            }
        }
    }


Solution

  • Actually I tried a lot and figured out the solution is adding cfg.addResource("Person.hbm.xml") statement to HibernateUtil.java:

     static {
             Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
              cfg.addResource("Person.hbm.xml");
              StandardServiceRegistryBuilder builder = new 
              StandardServiceRegistryBuilder()
                                .applySettings(cfg.getProperties());
                        sessionFactory = cfg.buildSessionFactory(builder.build());
                }