org.hibernate.MappingException: Unknown entity: annotations.Users

前端 未结 19 1488
情歌与酒
情歌与酒 2020-12-01 07:11

Consider the hierarchy :

\"enter

And the following classes and xml :

相关标签:
19条回答
  • 2020-12-01 07:51

    Add the following code to hibernate.cfg.xml

      <property name="hibernate.c3p0.min_size">5</property>
      <property name="hibernate.c3p0.max_size">20</property>
      <property name="hibernate.c3p0.timeout">300</property>
      <property name="hibernate.c3p0.max_statements">50</property>
      <property name="hibernate.c3p0.idle_test_period">3000</property>
    
    0 讨论(0)
  • 2020-12-01 07:52

    Instead of using HibernateUtil.java, to create sessionfactory object, you should use this:

    SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
    

    Because in order to avoid the exception, you'll have to declare the class object in HibernateUtil.java file as configuration.addAnnotatedClass(Student_Info.class); which looks dumb because we have provided the entry already in hibernate.cfg.xml file.

    To use the AnnotationConfiguration class you'll have to add a jar to your project build path: http://www.java2s.com/Code/Jar/h/Downloadhibernate353jar.htm

    0 讨论(0)
  • 2020-12-01 07:53

    I was having same problem.

    Use @javax.persistence.Entity instead of org.hibernate.annotations.Entity

    0 讨论(0)
  • 2020-12-01 07:55

    I was having similar issue and adding

     sessionFactory.setAnnotatedClasses(User.class);
    

    this line helped but before that I was having

    sessionFactory.setPackagesToScan(new String[] { "com.rg.spring.model" });
    

    I am not sure why that one is not working.User class is under com.rg.spring.model Please let me know how to get it working via packagesToScan method.

    0 讨论(0)
  • 2020-12-01 07:56

    The Hibernate configuration file must define the entity classes:

    <mapping class="annotations.Users"/>
    

    Or you must explicitly add the class to the configuration using

    configuration.addClass(annotations.Users.class)
    // Read mappings as a application resourceName
     // addResource is for add hbml.xml files in case of declarative approach
    configuration.addResource("myFile.hbm.xml");  // not hibernateAnnotations.cfg.xml
    
    0 讨论(0)
  • 2020-12-01 07:56

    In my case it resolves by adding configuration.addAnnotatedClass(com.myApp.model.Example.class); after Configuration configuration = new Configuration().configure(HibernateUtil.class.getResource("/hibernate.cfg.xml")); in hibernateUtill class. It Read a mapping from the class annotation metadata . Read more about addAnnotatedClass() from here.

    0 讨论(0)
提交回复
热议问题