Saturday, April 12, 2014

How to fix org.hibernate.MappingException: Unknown entity Exception in Java

If you have used Hibernate with JPA and using annotation to declare your entity bean then you might have seen this confusing error called "org.hibernate.MappingException: Unknown entity". This error message is so misleading that you could easily lose anywhere between few minutes to few hours looking at wrong places. I was using Spring 3 and Hibernate 3.6 when I got this error,which occurs when addEntity() method was executed. I checked everything, from Spring configuration file applicationContext.xml, Hibernate config file, my Entity class and DAO class to see whether my Entity class is annotated or not, but I was still getting this error message. After some goggling I eventually find that, it was an incorrect import which was causing this error.

Both hibernate and JPA has @Entity annotation and some how Eclipse was automatically importing org.hibernate.annotations.Entity instead of javax.persistence.Entity annotation. Once I fixed this import issue, everything went smooth.

Unfortunately, this error is not easy to spot, the org.hibernate.annotations.Entity import seemed completely appropriate to many programmers. For a newbie Java or hibernate developer it’s really hard to understand which Entity annotation to use, shouldn't be in org.hibernate.annotations.Entity, but instead it's javax.persistence.Entity.

By the way, if you are using auto-complete functionality of Eclipse IDE, then you can blame it them, alternatively you can configure your preferred import in Eclipse.



Error :

Exception in thread “main” org.hibernate.MappingException: Unknown entity: Person
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1365)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)

Version : Spring 3 and Hibernate 3.6

Cause : Incorrect import for @Entity annotation, My entity class was incorrectly annotated by org.hibernate.annotations.Entity annotation rather than javax.persistence.Entity, which comes with JPA.

Solution : Use javax.persistence.Entity to annotate your entity beans. Don't import org.hibernate.annotations.Entity


How to fix org.hibernate.MappingException: Unknown entity Exception in JavaThat's all about how to fix "Exception in thread “main” org.hibernate.MappingException: Unknown entity: Person" error in Hibernate, JPA and Spring based web application. Though it's one of the simplest fix you have to make, this can take anywhere between few minutes to few hours to actually find that this is due to wrong package or incorrect import of @Entity annotation. Another fact, which contribute to this error is similar/same names used by JPA and hibernate. The org.hibernate.annotations existed before JPA 1. JPA now exists and for new projects, you probably want to use it's annotations, though annoyingly they are often subtly different. It could have been better had the JPA folks could have used new names and they did in some cases, but the hibernate annotations, 4 years old before JPA1 came out were logical names.

Further Learning
Introduction To Hibernate
Spring with JPA and Hibernate
Hibernate Performance tuning

Other Hibernate Articles and Interview Questions you may like
  • Difference between First and Second level cache in Hibernate? (answer)
  • Difference between get() and load() method in Hibernate? (answer)
  • 5 Spring and Hibernate Training Courses for Java developers (list)
  • 2 Books to Learn Hibernate in 2017 (books)
  • 5 Books to Learn Spring Framework in 2017 (books)
  • Why Hibernate Entity class should not be final in Java? (answer)
  • 10 Hibernate Questions from Java Interviews (list)

Thanks for reading this article, if you like this article and interview question then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.

9 comments :

Anonymous said...

Hey. I have the same problem using Play framework 2 with submodules. As long my source was in main project it was ok. After moving code to /modules/common/app/models I have this error. I cant find out solution. Can You see here
http://stackoverflow.com/questions/25863415/how-to-persist-models-entity-in-playframework-submodule-using-jpa-hibernate/25863762#25863762

Javin Paul said...

@Anonymous, It looks more like a Classpath problem than anything else. When you move your code to /modules/common/app/models, did you also updated the package statement on your entity class? Just a guess, but worth checking ...

Anonymous said...

Just a note , in a standalone Hibernate (without using Spring) , this error occurs if you missed the Entity class entry in the hibernate.cfg.xml file like:-



Once you place entry as above for all your Entity classes, the error will be resolved.

Avatar Attupurath said...

I am using javax.persistence but I am still facing this error. Annoted the class and everything... What else can i look into ?

iy0o said...

verify your sessionFactory property annotatedClass/packageToScan

Anonymous said...

Thank you very much, iy0o!
I did typo in sessionFactory.setPackagesToScan(new String[] {"..."});

john said...

it worked for me..many thanks :)

Unknown said...

You saved me a week. Thank YOU

govinda babu said...

Exception in thread "main" org.hibernate.MappingException: An AnnotationConfiguration instance is required to use

Post a Comment