2016-03-07 176 views
0

此錯誤是使用Hibernate 3.2和解決存在的使用ServiceRegistryBuilder 這是我的代碼:異常線程 「main」 java.util.ServiceConfigurationError

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(); 
      } 
    } 

錯誤:

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) 

這是用錯誤的api執行hibernate框架時出錯。 這個問題通過改變這樣的我的代碼解析:

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(); 
} 
} 

這是代碼。

+2

問題是? – kosa

+0

我收到此錯誤,如何解決? –

+0

再次,你想做什麼? – kosa

回答

相關問題