Hibernate unknown entity (not missing @Entity or import javax.persistence.Entity )

I forgot to add the hbm.xml mapping into my cfg.xml file. As soon as I added it, this exception disappeared.

<mapping resource="com/mycompany/backend/hibernate/CServiceCenters.hbm.xml" />

Add to hibernate.cfg.xml before </session-factory> this:

<mapping class="org.assessme.com.entity.User" />

Consider using AnnotationConfiguration described here.

You could either use its addClass(User.class) method, or if you're persisting multiple entities use the addPackage("org.assessme.com.entity") method.

You're kind of reinventing the wheel with this HibernateUtil class. Consider using Spring so you can leverage something like its AnnotationSessionFactoryBean.


You should use AnnotationConfiguration instead of Configuration and then call

configuration.addAnnotatedClass(User.class);

to add your class to the list of mapped classes.

Tags:

Java

Hibernate