IllegalArgumentException : Unknown entity in Hibernate xml configuration

144 Views Asked by At

I am trying to test the Hibernate @Any mapping. But i am facing a problem : IllegalArgumentException: Unknown entity. I am using hibernate XML based configuration, in a maven project.

i have checked this question : Unknown Entity and also this one Unknown Entity 2

But i think they are not the things, i am looking for.

CFG file

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">
    jdbc:mysql://localhost:3306/concretepage</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">2993JGda</property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.hbm2ddl.auto">create</property>

    <mapping class="com.concretepage.entity.Boy"/>
    <mapping class="com.concretepage.entity.Girl"/>
    <mapping class="com.concretepage.entity.StudentInfo"/>
    <mapping class="com.concretepage.entity.College"/>
    <mapping package="com.concretepage.entity"/>
   </session-factory>
</hibernate-configuration>

Entity Classes

Student

package com.concretepage.entity;

public interface Student {
    String getName();
    int getAge();
}

Boy

@Entity
@Table(name="boy")
public class Boy implements Student {
    @Id
    private int id;
    @Column(name="name")
    private String name;
    @Column(name="age")
    private int age;
//getter-Setter

HibernateUtil

package com.concretepage;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
     private static SessionFactory sessionFactory ;
     static {
           Configuration configuration = new Configuration().configure();
           StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
                applySettings(configuration.getProperties());
           sessionFactory = configuration.buildSessionFactory(builder.build());
    }
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public static void closeSessionFactory() {
        sessionFactory.close();
    }
}

Main class/Execution class

public class HibernateManyToAnyDemo {
    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Boy boy = new Boy();
        boy.setId(1);
        boy.setName("Mukesh");
        boy.setAge(30);
        session.persist(boy);
        //other codes...

Now when i am running the program i am facing :

Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: com.concretepage.entity.Boy
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:713)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:696)
    at com.concretepage.HibernateManyToAnyDemo.main(HibernateManyToAnyDemo.java:21)

I am not understanding why it is happing. Another thing is that in the configuration i have given hibernate DDL update. so that table should be created in the database. But table is not being created there.

So far two questions i have seen according to them : have to use package-scan, but i think it is not needed in my example cause, i am mapping them in cfg file and i have also used @Entity annotation properly.

Any help, comment will be appreciated.

1

There are 1 best solutions below

0
On

You can debug into the configure method of new Configuration().configure() to see what is happening. Hibernate tries to find a file name hibernate.cfg.xml on the class path, so if you don't see the DDL export happening, the file is most likely not found. If you are using Maven to build your project, make sure the file is directly contained in src/main/resources, otherwise it will not be picked up.