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>
2
  • Show us the Java code you are using to initialize Hibernate. Commented Feb 26, 2016 at 6:51
  • I dont have any Java code other than Test.java class, which I have post above. Commented Feb 26, 2016 at 7:45

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Now I have to live with the fact I have done such a stupid mistake in my life.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.