ron ron - 4 years ago 349
Java Question

org.hibernate.MappingException: Unknown entity: annotations.Users

Consider the hierarchy :

enter image description here

And the following classes and xml :

package annotations;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

* @author X3
public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();
private static final String HIBERNATE_CFG = "hibernateAnnotations.cfg.xml";

private static SessionFactory buildSessionFactory()
Configuration cfg = new Configuration().addResource(HIBERNATE_CFG).configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
return sessionFactory;

public static SessionFactory getSessionFactory() {
return sessionFactory;

package annotations;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Timestamp;

@Table(name = "Users")
public class Users {

@Column(name = "USER_ID")
private long userID;

@Column(name = "USERNAME", nullable = false, length = 100)
private String username;

@Column(name = "MessageTimeDate", nullable = false)
private java.sql.Timestamp datetime;

@Column(name = "UserMessage", nullable = false)
private String message;

public Users(String username , String message)
java.util.Date date = new java.util.Date();
this.datetime = new Timestamp(date.getTime());

this.username = username;
this.message = message;

public long getUserID() {
return userID;

public void setUserID(long userID) {
this.userID = userID;

public String getUsername() {
return username;

public void setUsername(String username) {
this.username = username;

public java.sql.Timestamp getDatetime() {
return datetime;

public void setDatetime(java.sql.Timestamp datetime) {
this.datetime = datetime;

public String getMessage() {
return message;

public void setMessage(String message) {
this.message = message;


package annotations;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class Main {

public static void main(String[] args) {

SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();

Users user1 = new Users("Jack" , "Hello");;

catch (Exception e)


And hibernateAnnotations.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/CHAT_DB</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="">create</property>
<mapping class="annotations.Users"></mapping>

When I run main() , I get this :

org.hibernate.MappingException: Unknown entity: annotations.Users

But the entity
is in
package , so what's wrong ?

Much appreciated

Answer Source

The Hibernate configuration file must define the entity classes:

<mapping class="annotations.Users"/>

Or you must explicitly add the class to the configuration using

// Read mappings as a application resourceName
 // addResource is for add hbml.xml files in case of declarative approach
configuration.addResource("myFile.hbm.xml");  // not hibernateAnnotations.cfg.xml
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download