Hibernate – Error Message – “Unknown entity”

Background

Learning new tooling is not easy for me.

Error

Image

unknownEntity.20190509.0942AM

Textual

Unknown entity: sakila.Country

Trouble Shooting

Metadata

Entities Registered

Outline

  1. Start a Session or use an existing one
  2. Get Session’s Transaction handle
  3. Start a new transaction
  4. Get Session’s metamodel
  5. Get Metamodel entity list
  6. Iterate Metamodel entity list
  7. Commit Transaction

Code

/*
     * This function lists the entities declared in our session
     * It principally relies on session.getMetaModel.getEntities
     */
    private void entityList
    (
		  Session session
	)
    { 

    	String strEntity;
    	int i;
    	int iNumberofElements;
    	Transaction transaction;
    	Metamodel objEntityManager;
        Set<EntityType> objEntityTypeList;

    	try
        {

        	System.out.println("Retrieve entities ...."); 

        	//Get Session's Transaction
        	transaction
        		= session.getTransaction();

        	//Start Transaction
            transaction.begin();

            //Get MetaModel
            objEntityManager
            	= session.getMetamodel();

            //Get Entities
            objEntityTypeList
            	= objEntityManager.getEntities();

            //get Number of Entities
            iNumberofElements
            	= objEntityTypeList.size();

        	System.out.println
        		(
					CHAR_TAB
					+ "Number of elements is "
					+ iNumberofElements
				); 

        	//reset i
            i = 0;

            //Iterate through collection
            for (javax.persistence.metamodel.EntityType objEntityType:  objEntityTypeList)
            {

            	//increment
            	i = i +1;

            	//Get Entity
            	strEntity = objEntityType.toString();

            	//Display Entity
            	System.out.println
            	(
    				CHAR_TAB + CHAR_TAB
    				+ i
    				+ ") "
            	    + strEntity
        	    ); 

            }

        	System.out.println("Entities Retrieved"); 

        	//Close Transaction
            transaction.commit();

        }

    	catch (Exception ex)
    	{

    		System.out.println(ex.getMessage()); 

            if (session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }

        } //Iterate through collection catch

	} //entityList<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>

Similar Errors

@OneToOne or @ManyToOne on [entity] references an unknown entity

Image

@OneToOne or @ManyToOne on sakila.City.country references an unknown entity.

Text


Failed to create sessionFactory object.org.hibernate.AnnotationException:
@OneToOne or @ManyToOne on sakila.City.country references an unknown entity: sakila.Country

@OneToOne or @ManyToOne on sakila.City.country references an unknown entity: sakila.Country

Summary

Was able to confirm that the entities were not registering correctly.

And, moved on from there and sought help from a real Java developer.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s