0

I'm trying to test the hibernate configuration of a project, but when I try to run the test it throws a MappinException saying that it does not known my entity, I checked the database and it is not creating the tables either, so my guess is that it is not reading the mapping files, but I checked that and the path is correct. And the package and class properties inside the mapping files is also correct (supposely).

Most of the questions I read are Annotation Focused, does anyone has an idea what's happening under the hood?

Thanks in advance

I have the following directory structure:

Directory Structure

The hibernate.cfg.xml file it's like follows:

<hibernate-configuration>
    <session-factory>

        <!-- Hibernate Configuration -->
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="hibernate.hbm2ddl.auto">
            update
        </property>

        <!-- Database Configuration -->
        <property name="hibernate.connection.url">
            jdbc:mysql://127.0.0.1:3306/library_management?createDatabaseIfNotExist=true
        </property>
        <property name="hibernate.connection.username">
            root
        </property>
        <property name="hibernate.connection.password">
            cetys
        </property>

        <!-- Class Mapping Files -->
        <mapping resource="Hibernate.config/mapping/Book.hbm.xml"/>
        <mapping resource="Hibernate.config/mapping/Classification.hbm.xml"/>
        <mapping resource="Hibernate.config/mapping/Collection.hbm.xml"/>
        <mapping resource="Hibernate.config/mapping/Loan.hbm.xml"/>
        <mapping resource="Hibernate.config/mapping/NonExistentRegistry.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

The Loan mapping file is:

<hibernate-mapping package="com.cetys.cetyslibraryinventory.Core.DomainModels">
    <class name="Loan" table="loan">
        <meta attribute="class-description">
            This class contains the basic information for a Loan
        </meta>
        <id name="id" column="loan_id" type="int"></id>
        <many-to-one name="bookId" class="Book" unique="true"
                     column="book_id" cascade="all"/>  
    </class>
</hibernate-mapping>

The model:

package com.cetys.cetyslibraryinventory.Core.DomainModels;

//imports ommited

public class Loan implements Catalogable {

    private int id;

    private int book_id;

    protected Loan () {

    }

    public Loan ( int id, int book_id ) {
        this.id = id;
        this.book_id = book_id;
    }

    // Getters and Setters Ommited
}

And when I try to run the following:

     public class Program {

        public static void main ( String[] args ) {
            SessionFactory sessionFactory = null;

            Session session = null;

            Transaction tx = null;

            try {
                Configuration configuration = new Configuration();

                configuration.configure( "hibernate.cfg.xml" );

                ServiceRegistry serviceRegistry
                        = new StandardServiceRegistryBuilder().applySettings(
                                configuration.getProperties() ).build();
            sessionFactory = configuration.
                    buildSessionFactory( serviceRegistry );
            session = sessionFactory.openSession();

            Loan ln = new Loan( 0, 0 );

            tx = session.beginTransaction();
            session.save( ln );

            tx.commit();
        } catch ( Exception e ) {
            LOGGER.log( Level.SEVERE, e.getMessage(), e );
        } finally {
            if ( session != null ) {
                session.close();
            }
            if ( sessionFactory != null ) {
                sessionFactory.close();
            }
        }
    }

}

Exception Error:

SEVERE: Unknown entity: com.cetys.cetyslibraryinventory.Core.DomainModels.Loan
org.hibernate.MappingException: Unknown entity: com.cetys.cetyslibraryinventory.Core.DomainModels.Loan
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1533)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:682)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:674)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:669)
    at com.cetys.cetyslibraryinventory.Program.main(Program.java:74)

Jun 08, 2016 10:04:41 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://127.0.0.1:3306/library_management?createDatabaseIfNotExist=true]
2
  • Did you try this stackoverflow.com/a/23215605/3344829 ? Commented Jun 8, 2016 at 18:10
  • That works for annotated classes, I'm handling the mapping with xml. Commented Jun 8, 2016 at 18:10

0

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.