Hibernate代码在获取sessionfactory对象时给出异常

时间:2016-04-12 11:05:45

标签: hibernate

private static SessionFactory factory; 

    public static void main(String[] args) {
        try{
            factory = new Configuration().configure().buildSessionFactory();
        }catch (Throwable ex) { 
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex); 
        }
      ManageEmployee ME = new ManageEmployee();

这是我的代码。在线

factory = new Configuration().configure().buildSessionFactory();

抛出错误:

Failed to create sessionFactory object.java.util.ServiceConfigurationError: org.hibernate.boot.registry.selector.StrategyRegistrationProvider: Provider org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl not found
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.sanket.ManageEmployee.main(ManageEmployee.java:22)
Caused by: java.util.ServiceConfigurationError: org.hibernate.boot.registry.selector.StrategyRegistrationProvider: Provider org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl not found
    at java.util.ServiceLoader.fail(Unknown Source)
    at java.util.ServiceLoader.access$300(Unknown Source)
    at java.util.ServiceLoader$LazyIterator.next(Unknown Source)
    at java.util.ServiceLoader$1.next(Unknown Source)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:340)
    at org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder.buildSelector(StrategySelectorBuilder.java:162)
    at org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:222)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
    at com.sanket.ManageEmployee.main(ManageEmployee.java:19)

可能是什么问题?我试图谷歌它有一些新的方法来获得对象,但仍然是同样的问题。

public static void main(String[] args) {

    try{
        Configuration configuration = new Configuration().configure();
        configuration.configure("hibernate.cfg.xml");
        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = ssrb.build();
        setFactory(configuration.buildSessionFactory(serviceRegistry));
    }catch (Throwable ex) { 
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex); 
    }

根据谷歌的建议,这是新代码。

有人可以帮助我吗?我对Hibernate完全不熟悉。

1 个答案:

答案 0 :(得分:0)

这不是配置代码问题。此代码应该可以正常运行

factory = new Configuration().configure().buildSessionFactory();

此课程org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl

的问题

它用于与Infinispan缓存集成,并位于相应版本的hibernate-infinispan-xxx.jar中。

您需要将jar添加到http://mvnrepository.com/artifact/org.hibernate/hibernate-infinispan/

的类路径中

或者您可以从Hibernate配置中删除infinispan缓存配置,例如来自hibernate.cfg.xml

<强>更新

Hibernate 4不推荐使用

new Configuration().configure().buildSessionFactory()代码。对于Hibernate 5,不推荐使用它。很有趣的是,修复该错误后,配置代码的第二个变体将无效: Hibernate 5 :- org.hibernate.MappingException: Unknown entity

错误原因

org.hibernate.boot.registry.selector.StrategyRegistrationProvider: Provider org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl not found

java.util.ServiceLoader无法加载课程org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl

您可以通过在配置代码

之前添加此代码来检查此类
 URL url = Thread.currentThread().getContextClassLoader()
    .getResource(
    "org/hibernate/cache/infinispan/StrategyRegistrationProviderImpl.class");
System.out.println(url);

你有一个使用main()方法的控制台应用程序,并在WEB-INF/lib中有一个jar,这很奇怪。