Traditional Hibernate Issues (POJO): Part#1 org.hibernate.MappingException: Unknown entity

Unknown Entity exception might have a verity of reasons and causes, however, when you’re using POJO and XML files, since there is no Entity tags and mismatching between JPA nad Hibernate class tags, the reason of this exception might be a little bit challenging. when you’re using POJO with newer version of Hibernate, the main reason might be related to the way you are initiating SessionFactory.something like below…

 

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
 org.hibernate.MappingException: Unknown entity: com.navid.practice.hibernate.User
 at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
 at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1605)
 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:674)
 at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
 at org.hibernate.internal.SessionImpl.save(SessionImpl.java:661)
 at com.navid.practice.hibernate.Program.main(Program.java:23)

the issue was related to the version of Hibernate that I had, 5.x and the code that I wrote for init the SessionFactory:

public class HibernateUtilities {

private static ServiceRegistry svcReg = null;
 private static SessionFactory sessionFactory = null;

static {

try {
 Configuration cfg = new Configuration().configure();

svcReg = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
 sessionFactory = cfg.buildSessionFactory(svcReg);

} catch (HibernateException e) {
 System.out.println("An Error in init the SessionFactory!");
 }

}

Solution:

the issue could be resolved just by changing the SessionFactory creation line …

sessionFactory = cfg.buildSessionFactory();
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s