How to resolve NoSuchFieldError : namingStrategy in Hibernate

When configuring and using Hibernate in your application you might have surely came across the error which says java.lang.NoSuchFieldError : namingStrategy. The full stack trace of the error would be of the form:

Exception in thread “main” java.lang.NoSuchFieldError: namingStrategy
at org.hibernate.cfg.AnnotationConfiguration.reset(AnnotationConfiguration.java:237)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:125)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
at org.hibernate.cfg.AnnotationConfiguration.<init>(AnnotationConfiguration.java:96)
at com.codippa.Main.main(Main.java:12)

and the code which is generating this error would be as given below :

AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.configure("hibernate.cfg.xml");
cfg.addAnnotatedClass(Employee.class);
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();

Solution :

There may be multiple solutions of this error.

      1. Try replacing this line :
        AnnotationConfiguration cfg = new AnnotationConfiguration();

        with this

        Configuration cfg = new Configuration();
        

        Reason : Starting Hibernate v3.6, the class org.hibernate.cfg.AnnotationConfiguration has been Deprecated. All its functionality has been moved to org.hibernate.cfg.Configurationclass. So if you are using the above code, then chances are that the Hibernate version being utilized in your application is 3.6 or above. Refer this link.

      2. Check your dependencies. You might have two different versions of Hibernate. For example, if you are using Maven, then you may find something like below in your pom.xml.
        <dependency>
              <groupId>hibernate</groupId>
              <artifactId>hibernate-entitymanager</artifactId>
              <version>3.4.0.GA</version>
        </dependency>
        <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate-entitymanager</artifactId>
              <version>3.6.10.Final</version>
        </dependency>
      3. Try removing the slf4j dependency (if you have) from your build path. If you are using Maven, then modify your Hibernate dependency to :
        <dependency>
              <groupId>hibernate</groupId>
              <artifactId>hibernate-entitymanager</artifactId>
              <version>3.6.10.FINAL</version>
              <exclusions>
                <exclusion>
                  <artifactId>org.slf4j</artifactId>
                  <groupId>slf4j-api</groupId>
                </exclusion>
              </exclusions>
         <scope>compile</scope>
         </dependency>

        If the build tool is Gradle then something like that given below might help :

        dependencies {
          compile ('org.hibernate:hibernate:3.1') {
          exclude group: "org.slf4j", name: "slf4j-log4j12"
          }
        }

      Did any of the above methods resolve your error ? Wonderful !!! Let others also resolve this problem by your sharing

2 Comments

  1. There are many solutions available for this problem but none of them had the reasons behind those solutions. Now I understand why those solutions work. Thanks

Leave a Reply