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

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 a few minutes to a few hours looking at the 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 googling, I eventually find that it was an incorrect import that was causing this error.

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

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 the auto-complete functionality of Eclipse IDE, then you can blame it on 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 If you are new to JPA, you can further see these Hibernate and JPA online courses to learn more about Java Persistence API. 


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 fixes you have to make, this can take anywhere between few minutes to few hours to actually find that this is due to the wrong package or incorrect import of @Entity annotation. Another fact, which contributes 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.


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 for Beginners (books)
  • 5 Books to Learn Spring Framework for Java developers(books)
  • Why Hibernate Entity class should not be final in Java? (answer)
  • 10 Hibernate Questions from Java Interviews (list)
  • My Favorite Hibernate and JPA courses for Beginners (online courses)
Thanks for reading this article, if you like this article and the interview question then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment.

12 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.

TheBaldSpot 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

Govindh said...

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

Ramkishna Sitap said...

Simple solution : If you work with Spring Framework then just change in your Dao & Service layer method's parameters type. simply replace your 'Integer' parameter to 'Object' parameter. Eg : public void delete(int id) To replace public void delete(User user). your 'Unknown entity: java.lang.Integer' error will solve...

ManWithCode said...

Hi Paul am getting the following error too Caused by: java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested along with stated above "Unknown Entity " Exception it will be helpful if you provide us with insight of relation between these exceptions.

Anonymous said...

I have this problem but adding an @Entity annotation is not an viable option. My POJO object does not have an id. Query results are just aggregated statistivcs and id is irrelevant here.

Post a Comment