[Solved]org.hibernate.MappingException: Unknown entity: XYZ


“org.hibernate.MappingException: Unknown entity” this exception occurred when you are using an entity in Hibernate session but with respect to that entity mapping file (hbm.xml) is not defined or used an POJO class with out any JPA/Hibernate annotations.

Example


org.hibernate.MappingException: Unknown entity: com.facingissuesonit.model.Stock
    at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1634)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:689)

Solutions

In my case, I used an entity Stock in hibernate session but forget to add my Stock.hbm.xml file mapping in hibernate.cfg.xml file. When I added this line of statement in my hibernate.cfg.xml then problem resolved.

Problem: Hibernate was trying to find out the mapping for used entity Stock.java in all defined XML and not able to get that’s what throw exception.
Add this line in hibernate.cfg.xml



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s