MappingException: Unknown Entity on save() after migration

Hi everyone,

This is probably a stupid mistake somewhere in my code but I’ve spent so much time and still haven’t found what is wrong, so here it goes.

I’m migrating a tool for automating prefetching in databases, so in the initialization, I am using a different type of configuration.
I migrated from Hibernate 3.2 to 4.3, and in order to test the changes for the migration I’m testing on a test project. For the old version, everything was working accordingly. Now, however, there seem to be something wrong with how the config reads the hibernate.cfg.xml, because it seems that my test entity cannot be found. I suspect that it might have something to do with the way I initialize the

I’m using the following configuration:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">Werty112</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping resource="com/mkyong/common/Stock.hbm.xml"></mapping>

public class Stock implements {

	private static final long serialVersionUID = 1L;

	private Integer stockId;
	private String stockCode;
	private String stockName;

	public Stock() {

	public Stock(String stockCode, String stockName) {
		this.stockCode = stockCode;
		this.stockName = stockName;

	public Integer getStockId() {
		return this.stockId;

	public void setStockId(Integer stockId) {
		this.stockId = stockId;

	public String getStockCode() {
		return this.stockCode;

	public void setStockCode(String stockCode) {
		this.stockCode = stockCode;

	public String getStockName() {
		return this.stockName;

	public void setStockName(String stockName) {
		this.stockName = stockName;



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    <class name="com.mkyong.common.Stock" table="stock" catalog="sys">
        <id name="stockId" type="java.lang.Integer">
            <column name="STOCK_ID" />
            <generator class="identity" />
        <property name="stockCode" type="string">
            <column name="STOCK_CODE" length="10" not-null="true" unique="true" />
        <property name="stockName" type="string">
            <column name="STOCK_NAME" length="20" not-null="true" unique="true" />

Main method:

    public static void main( String[] args )
        System.out.println("Maven + Hibernate + MySQL");
        Session session = HibernateUtil.getSessionFactory().openSession();

        Stock stock = new Stock();
        stock.setStockName("E");;  //breaks here

The initialization of the config:

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            AutofetchConfiguration cfg = (AutofetchConfiguration) new AutofetchConfiguration();
            StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
            ExtentManager extentManager = new ExtentManager();
            ssrb.addService(EventListenerRegistry.class, new AutofetchEventListenerRegistryImpl(extentManager));
            return cfg.buildSessionFactory(, extentManager);
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);

    public static SessionFactory getSessionFactory() {
        return sessionFactory;

    public static void shutdown() {
    	// Close caches and connection pools


The error:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.mkyong.common.Stock
	at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(
	at org.hibernate.internal.SessionImpl.getEntityPersister(
	at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(
	at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(
	at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(
	at org.hibernate.internal.SessionImpl.fireSave(
	at com.mkyong.common.App.main(

Does anyone have an idea what could be wrong here?
Thanks in advance.

I have tried reverting to the normal configuration, and that works. So there are some errors in the AutofetchConfiguration apparently. Anyone who might have ideas about what could be wrong with the configuration-class if the entities are not stored properly?

This was solved by adding the call to add(resource) which was missing before. Now it finds the mappings correctly :slightly_smiling_face:


even my code breaks at could u show me the place where u made the changes…