Himal Acharya Himal Acharya - 1 month ago 5x
Java Question

Exception in Mapping in Hibernate -Unknown Entity

I just write simple program for demo of hibernate. But I got Mapping exception.

hibernate.cfg.xml is under src directory

<?xml version='1.0' encoding='utf-8'?>
~ Hibernate, Relational Persistence for Idiomatic Java
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"



<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>

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

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

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</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>

<!-- Names the annotated entity class -->
<mapping class="hibernateProject.com.himal.hibernate.HibernateTest"/>



Under src/hibernateProject.com.himal.hibernatePractise.dto, I create UserDetails class.

package hibernateProject.com.himal.hibernatePractise.dto;

import javax.persistence.Entity;
import javax.persistence.Id;

* Created by Himal Acharya on 2016-09-01.

public class UserDetails {

//for primary key
private int userId;
private String userName;

public int getUserId() {
return userId;

public void setUserId(int userId) {
this.userId = userId;

public String getUserName() {
return userName;

public void setUserName(String userName) {
this.userName = userName;

And my Test class is

package hibernateProject.com.himal.hibernate;

import hibernateProject.com.himal.hibernatePractise.dto.UserDetails;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

* Created by Himal Acharya on 2016-09-01.
public class HibernateTest {

public static void main(String[] args) {
//Initiate object of userDetails
UserDetails user=new UserDetails();

//setting value

//get the configuration

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();

//Ending Transcation


But I got following error:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: hibernateProject.com.himal.hibernatePractise.dto.UserDetails

Error is at hibernateProject.com.himal.hibernate.HibernateTest.main as stated in cosole in line session.save(user);


At your hibernate.cfg.xml, you have to map the entity:

<mapping class="hibernateProject.com.himal.hibernatePractise.dto.UserDetails "/>