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

This is my code. on line

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

its throwing as error:

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)

what might be the problem ? I tried to google it there is some new way to get the object but still same problem.

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

this is the new code as per suggestions on google.

Can someone help me with it ? I am totally new to Hibernate.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67

1 Answers1

0

It is not a configuration code problem. This code should work well

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

A problem with this class org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl

It is used for integration with Infinispan cache and resides in the hibernate-infinispan-xxx.jar of corresponding version.

You need to add jar to the class path from http://mvnrepository.com/artifact/org.hibernate/hibernate-infinispan/

Or you can remove a infinispan cache configuration from a Hibernate configuration, for an example from hibernate.cfg.xml.

Update

new Configuration().configure().buildSessionFactory() code is deprecated for Hibernate 4. It is not deprecated for Hibernate 5. It is funny that your second variant of a configuration code will not work after you fix that error: Hibernate 5 :- org.hibernate.MappingException: Unknown entity.

A reason of the error

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

that java.util.ServiceLoader can't load a class org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl

You can check this class by adding this code before the configuration code

 URL url = Thread.currentThread().getContextClassLoader()
    .getResource(
    "org/hibernate/cache/infinispan/StrategyRegistrationProviderImpl.class");
System.out.println(url);

You have a console application with the main() method and have a jar in the WEB-INF/lib, it is strange.

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • My hibernate version is 5.1.0. And I have seen many answers regarding the code factory = new Configuration().configure().buildSessionFactory(); this has been deprecated and the new lines I mentioned are used now. and I already have that jar in my lib folder under WEB-INF. – sanketprabhune Apr 12 '16 at 17:18
  • @PrithiviRaj You need to have `org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl ` class in the class path. Or you need to find something, that adds `infinispan` cache and remove it. – v.ladynev Jul 28 '17 at 08:11