java.lang.NoSuchFieldError:namingStrategy [英] java.lang.NoSuchFieldError: namingStrategy

查看:439
本文介绍了java.lang.NoSuchFieldError:namingStrategy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我放弃了,我需要你的帮助。

Ok, I give up, I need your help.

学习Hibernate。现在有一个例子来自如何在Hibernate中使用注释(在此之前我已经使用了相同的示例,但它使用了.xml而不是。我已经设法让它无异常地工作)。
现在我已经

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
      session.beginTransaction();
      Long result = (Long) session.save(subject);
      session.getTransaction().commit();
      return result;
    }
}



public class Test {

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

        Subject subject = new Subject();
        subject.setSubjectName("Mathematics");
        dao.addSubject(subject);//Test.java:12

    }
}


推荐答案

您好Kleeo

您已写完以下内容行 HibernateUtil 类。

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

这篇关于java.lang.NoSuchFieldError:namingStrategy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆