Tag Archives: org.hibernate.MappingException

[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