0

I am getting the below error while running my Hibernate program on Hibernate 5.0.1:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.abhinav.hibernate.UserDetails
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1542)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:225)
at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:499)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:99)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:778)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:751)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:756)
at com.abhinav.hibernate.main.HibernateTest.main(HibernateTest.java:26)

Below is the configuration file:

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
    <property name="connection.username">postgres</property>
    <property name="connection.password">password</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>


    <mapping resource="com.abhinav.hibernate.UserDetails"/>

</session-factory>

The main class:

package com.abhinav.hibernate.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.abhinav.hibernate.UserDetails;

public class HibernateTest {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    UserDetails userDetails = new UserDetails();

    userDetails.setUserId(1);
    userDetails.setUserName("Abhinav");

    Configuration configuration = new Configuration().configure();
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());     
    SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
    Session session = sessionFactory.openSession();
    session.getTransaction();
    System.out.println("Session: "+session);

    // session.save(userDetails);
    session.persist(userDetails);

    System.out.println("Session after save: "+session+" User details: "+userDetails.getUserId()+" "+userDetails.getUserName());
session.getTransaction().commit();
}

Entity Class: UserDetails with fields userId and userName.

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Abhinav
  • 21
  • 4

1 Answers1

1

There are two problems there

You need to use

<mapping class="com.abhinav.hibernate.UserDetails"/>

if you use annotations for mapping (resource is used for xml mappings).

You can't use Hibernate 4 way for building a session factory for Hibernate 5. The correct way:

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

Refer for details

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

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • Does session.getTransaction().commit() work with hibernate 5.0.1? I am able to create the table but since i am getting an error **org.hibernate.TransactionException: Transaction not successfully started at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:59) at com.abhinav.hibernate.main.HibernateTest.main(HibernateTest.java:30)** – Abhinav Feb 28 '16 at 07:10
  • Resolved the issue using Transaction class explicitly instead of using the transaction methods directly and using the org.hibernate.cfg.Transaction – Abhinav Feb 28 '16 at 07:38
  • @Abhinav It was not `session.getTransaction().commit()` problem. You didn't have any transaction on that point, because of `session.getTransaction()` does nothing. It just returns a current transaction that doesn't exist. – v.ladynev Feb 28 '16 at 18:33