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(
at org.hibernate.impl.SessionImpl.getEntityPersister(
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(
at org.hibernate.impl.SessionImpl.fireSave(

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.


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

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

Post a Comment