简体   繁体   中英

'Unknown Entity Exception' in Hibernate

I am working on Hibernate 3 project and facing the

'Unknown Entity' exception

. I have all the classes and those are mapped to the Table by using annotations. I have made all the required entries in the default ' hibernate.cfg.xml ' file.

I am getting an 'Unknown Entity Exception' and trying to figure why am I getting the same even though I have all the required configurations accurate.

I have included the correct package for the '@Entity' annotation which is: ' javax.persistence.Entity ' and have also provided the complete class name for the entry 'mapping class' in the 'hibernate.cfg.xml' file.

Anybody knows what are any other causes of exception?

Following is the entity class:

package com.myproj;
import java.io.serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="STUDENT")
public class Student implements Serializable{
    @Id
    @Column(name="student_id")
    private Long studentId;

    @Column(name="student_name")
    private Long studentName;

    //getters and setters

}

Snippet to create SessionFactory

SessionFactory sf;
Configuration config = new AnnotationConfiguration();
sf = config.configure().buidSessionFactory();

Student student = null;
Session session = sf.openSession();
student = (Student)session.get(Student.class, new Integer(1)); //Unknown Entity Exception thrown over here while trying to retrieve a Student with ID 1.

hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    <!-- All the data connection entries are provided here -->
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <mapping class="com.myproj.Student" /> 
</session-factory>
</hibernate-configuration>

Note that all the data connection entries like are provided

Stacktrace for the exception:

org.hibernate.MappingException: Unknown entity: com.myproj.Student
org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:629)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:91)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:908)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:845)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:838)
com.myproj.StudentBean.search(StudentBean.java:50)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)

There must be a typo in your hibernate config and actual class name. In what you provided you have

com.myproj.Student

and

<mapping class="com.proj.Student" /> 

so I guess you have anonimized it by hand thus I suspect you have similar typo in real class names.

I would suggest to start with using JPA2.* insteed of plain Hibernate. You can still use it as your presistence provider.

You mentioned that you were using Hibernate 3, but not which minor version, so I will first point out a couple things that may help since it appears you are early in your project and may have some flexibility. However, my answer will not stray from Hibernate 3 details.

  • Annotations support added in Hibernate 3.2
  • AnnotationConfiguration appears to have been added in Hibernate 3.5 (so I assume you are using Hibernate 3.5+)
  • AnnotationConfiguration was deprecated in Hibernate 3.6, and all equivalent functionality is present in Configuration
  • The current version is now Hibernate 5.2.12

You have a typo in the following line, but I assume that is not present in your code or you would have a compilation error versus a MappingException :

sf = config.configure().buidSessionFactory();  
// should be sf = config.configure().buildSessionFactory();

I would suggest the following:

  • Confirm that your configuration file name is exactly hibernate.cfg.xml and has no typo
  • Confirm that hibernate.cfg.xml is in your CLASSPATH
  • You may try explicitly specifying the path to hibernate.cfg.xml

For example,

// Hibernate 3.5
sf = new AnnotationConfiguration()
        .configure("/path/to/hibernate.cfg.xml")
        .buildSessionFactory();

// Hibernate 3.6+
sf = new Configuration()
        .configure("/path/to/hibernate.cfg.xml")
        .buildSessionFactory();

As an example, in my project I am using Maven, so my configuration file is located at <path/to/project>/<project_name>/src/main/resources/hibernate.cfg.xml , so I do not have to explicitly provide the path.

You may have omitted the XML DTD data intentionally for brevity, and I don't know that this would have anything to do with the MappingException you're seeing, but make sure the XML DTD is in your hibernate.cfg.xml file:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="...">...</property>
        <mapping class="..."/>
    </session-factory>
</hibernate-configuration>

The following is redundant to the <mapping /> element in your hibernate.cfg.xml file, and I hesitate to make this recommendation because if you don't get the parsing of <mapping /> elements in hibernate.cfg.xml working you will have to do this for any and all annotated POJOs you want to persist. However, as an absolute last resort you could try addAnnotatedClass() :

// Hibernate 3.5
sf = new AnnotationConfiguration()
        .addAnnotatedClass(Student.class)
        .configure()
        .buildSessionFactory();

// OR Hibernate 3.6+
sf = new Configuration()
        .addAnnotatedClass(Student.class)
        .configure()
        .buildSessionFactory(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM