Exception in thread “main” java.util.ServiceConfigurationError [closed]

狂风中的少年 提交于 2019-12-25 06:34:02

问题


This error is occuring with Hibernate 3.2 and resolved by using ServiceRegistryBuilder This is my code:

public class HibernateTest {
    public static void main(String[] args) {
    UserDetails user = new UserDetails();
    user.setUserId(1);
    user.setUserName("Sam");
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
          }
     } 

Error:

Exception in thread "main" java.util.ServiceConfigurationError: org.hibernate.boot.registry.selector.StrategyRegistrationProvider: Provider org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl not found
    at java.util.ServiceLoader.fail(ServiceLoader.java:231)
    at java.util.ServiceLoader.access$300(ServiceLoader.java:181)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:365)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:445)
    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)

This was the error while executing the hibernate framework with wrong api's. This problem was resolved by changing my code like this:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateTest {
private static SessionFactory sessionFactory;
public static void main(String[] args) {
    UserDetails user = new UserDetails();
    user.setUserId(1);
    user.setUserName("Sam");
     if (sessionFactory == null) {
            Configuration configuration = new Configuration().configure();
            ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
            registry.applySettings(configuration.getProperties());
            ServiceRegistry serviceRegistry = registry.buildServiceRegistry();  
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);           
        }

    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
}
}

This was the code.


回答1:


You have different version of Hibernate. Probably 4 and above. According to guideline you should use following syntax

http://www.codejava.net/frameworks/hibernate/building-hibernate-sessionfactory-from-service-registry



来源:https://stackoverflow.com/questions/35853341/exception-in-thread-main-java-util-serviceconfigurationerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!