Sunday, April 14, 2013

Resolve the error org.hibernate.MappingException: Unknown entity


I was facing this issue when I started implementing my first hibernate application. I have tried to find the solution over the technical forums. But I could not really get the answer which has fixed the issue. So I am posting the solution which would help you too. Reason: hibernate.cfg.xml does not have the resource mapping where the entity mapping is done.
 


Resolution: Add the mapping file name (where entity mapping is done) to the hibernate.cfg.xml .Highlighted in red color below.
 
   <session-factory>
   <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/HibDB</property>
    <property name="connection.username">root</property>
    <property name="connection.password">pass123</property>
    <mapping resource="com\sabhahit\mapping\Student.hbm.xml"/>
  </session-factory>
 



 Stractrace:org.hibernate.MappingException: Unknown entity: com.sabhahit.objects.Student                at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)                at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1302)                at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:89)                at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)                at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)                at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)                at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)                at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)                at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)                at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)                at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)                at com.sabhahit.utils.BasicCRUDOperations.doCRUDOperations(BasicCRUDOperations.java:30)                at com.sabhahit.utils.FirstHibernateApp.main(FirstHibernateApp.java:31) 

2 comments:

  1. hey where should we save hibernate.cfg.xml inside package or src folder only

    ReplyDelete
  2. hey where should we save hibernate.cfg.xml inside package or src folder only

    ReplyDelete

Resolve "Target runtime Apache Tomcat v6.0 is not defined." error

Go to window ->preferences ->Server->Runtime environments . add server Apache Tomcat v6.0 Mention the directory where apache-toma...