Kleeo Kleeo - 1 year ago 187
Java Question

java.lang.NoSuchFieldError: namingStrategy

Ok, I give up, I need your help.

Learning Hibernate. Now there's an example from net of how to use annotations in Hibernate(before that I've worked on the same example, but it used .xml instead. And I've managed to make it work without exceptions).
So now I have

Initial session factory creation failedjava.lang.NoSuchFieldError: namingStrategy
Exception in thread "main" java.lang.ExceptionInInitializerError
at firstproject.HibernateUtil.<clinit>(HibernateUtil.java:14)
at firstproject.StudentDAO.addSubject(StudentDAO.java:82)
at firstproject.Test.main(Test.java:12)
Caused by: java.lang.NoSuchFieldError: namingStrategy
at org.hibernate.cfg.AnnotationConfiguration.reset(AnnotationConfiguration.java:250)
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:108)
at firstproject.HibernateUtil.<clinit>(HibernateUtil.java:11)
... 2 more

Here is some code, that may help

public class HibernateUtil {
private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();//HibernateUtil.java:11
} catch (Throwable ex) {
System.err.println("Initial session factory creation failed" + ex);
throw new ExceptionInInitializerError(ex);//HibernateUtil.java:14

public static SessionFactory getSessionFactory() {
return sessionFactory;

public class StudentDAO{
public Long addSubject(Subject subject) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();//StudentDAO.java:82
Long result = (Long) session.save(subject);
return result;

public class Test {

public static void main(String[] args) {
StudentDAO dao = new StudentDAO();

Subject subject = new Subject();


Answer Source

Hi Kleeo

You have written the following line in HibernateUtil class.

sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

Replace this line of code with the below written line & retry. I hope this will work for you.

sessionFactory = new Configuration().configure().buildSessionFactory();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download