2

I am getting following exception on eclipse console

Exception in thread "main" org.hibernate.MappingException: Unknown entity: bitronix.examples.hibernate.entities.User

My hibernate.cfg.xml is like:

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">postgres</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/bitronixH4</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <mapping resource="bitronix/examples/hibernate/entities/User.hbm.xml" />
    </session-factory>
</hibernate-configuration>

My Test.java class is like

package bitronix.examples.hibernate.entities;

public class Test {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        SessionFactory sf1 = new Configuration()
                .buildSessionFactory(serviceRegistry);
        Session session = sf1.openSession();
        session.beginTransaction();
        User user = new User();
        user.setName("rrr");
        session.save(user);
        session.getTransaction().commit();
    }
}

My project structure

Any suggestion or help is appreciated.

EDIT: My User.java is like this

public class User {
private Integer id;
private String name;
//getters and setters
}

My User.hbm.xml is like this

<hibernate-mapping >
    <class name="bitronix.examples.hibernate.entities.User" table="test_user" >
        <id name="id" type="int">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" />
        </property>
    </class>
</hibernate-mapping>
Bhushan
  • 1,489
  • 3
  • 27
  • 45

1 Answers1

1

The problem, obviously, with configuration code. You create Configuration twice.

Just do

SessionFactory sf1 = new Configuration().configure().buildSessionFactory();

I know, that it is deprecated in Hibernate 4. But for Hibernate 5 it is a good way. This Hibernate 4 approach will not work with Hibernate 5.

Hibernate 5 :- org.hibernate.MappingException: Unknown entity

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • Now I have to live with the fact I have done such a stupid mistake in my life. – Bhushan Feb 26 '16 at 08:08
  • @BhushanPatil Everybody can make a mistake with configuration. Hibernate has a very inconvenient API and an unclear documentation. Try to write as little code as possible. There are a lot of people here using a lot of unnecessary annotations, for an example. – v.ladynev Feb 26 '16 at 08:10