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

前端 未结 6 1401
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 13:22

I\'ve got a really simple class:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.pers         


        
相关标签:
6条回答
  • 2020-12-16 13:31

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

    <mapping class="org.assessme.com.entity.User" />
    
    0 讨论(0)
  • 2020-12-16 13:34

    You should use AnnotationConfiguration instead of Configuration and then call

    configuration.addAnnotatedClass(User.class);
    

    to add your class to the list of mapped classes.

    0 讨论(0)
  • 2020-12-16 13:38

    Add below line to your hibernate.cfg.xml

    <mapping class="fully qualified Entity class name"/>
    
    0 讨论(0)
  • 2020-12-16 13:42

    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" />
    
    0 讨论(0)
  • 2020-12-16 13:50

    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.

    0 讨论(0)
  • 2020-12-16 13:50

    Do you have any persistence.xml or hibernate.cfg.xml? If you have to add your mapping class in it so hibernate get understand your class is mapping to the database. If you have hibernate.cfg.xml you can add like this

    <mapping class="com.entity.Users"/>
    
    0 讨论(0)
提交回复
热议问题