hibernate 出现org.hibernate.MappingException: Unknown entity的原因

原因在于hibernate5 与hibernate4创建 sessionFactory 的方式不同 

如果是hibernate5的jar  用hibernate4创建sessionFactory的方式创建sessionFactory就会出现org.hibernate.MappingException: Unknown entity的异常

hibernate4创建步骤:

    Configuration conf = new Configuration().configure();

    //服务注册,这是使用创建者模式,根据配置文件中的配置字段来构建注册服务(这应该是hibernate架构中注册服务的通用流程)。

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
    applySettings(conf.getProperties()).build();

    //使用实例化好了的注册服务,使用Configuration中的工厂模式实例化了SessionFactory
    SessionFactory sf = conf.buildSessionFactory(serviceRegistry);

hibernate5 创建步骤:

    StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();

    Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder()
    .applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build();

    //最后由这个metadata使用构建出sessionFactory
    SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();


猜你喜欢

转载自blog.csdn.net/yishuihanxun/article/details/80629275