Hibernate 5 :- org.hibernate.MappingException: Unknown entity

  • the method that i buid a sessionFactory is followed ,which will cause the MappintException of Unknown entity
 Configuration configuration = new Configuration();
        configuration.configure();
        ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

        SessionFactory sf = configuration.buildSessionFactory(sr);
  • Solution——- remove the codes of serviceRegistry
configuration.configure();
//serviceRegistry = new StandardServiceRegistryBuilder()
 //          .applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory();
  • Analyse
    the first code works fine for Hibernate 4.3.5, but the same code has the same issue for Hibernate 5.
    When you do configuration.buildSessionFactory(sr), using Hibernate 5, Configuration losts all information about mapping that gets by call configuration.configure().
    So you should use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

instead.

你可能感兴趣的:(Hibernate,hibernate,Exception)