简体   繁体   English

java.lang.NoSuchFieldError:namingStrategy

[英]java.lang.NoSuchFieldError: namingStrategy

There's an example from the web on 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). 网络上有一个示例,说明如何在Hibernate中使用批注(在我处理同一示例之前,但它使用了.xml。而且我设法使其无例外地工作)。 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
    }
}

Hi Kleeo 嗨克莱欧

You have written the following line in HibernateUtil class. 您已经在HibernateUtil类中编写了以下行。

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

AnnotationConfiguration has been Deprecated in Hibernate 3.6. Hibernate 3.6中已弃用AnnotationConfiguration。

As you can see in the documentation (see link below) all functionality has been moved to Configuration. 如您在文档中所见(请参见下面的链接),所有功能已移至“配置”。

You can use safely Configuration instead. 您可以安全地使用配置。

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

AnnotationConfiguration documentation: AnnotationConfiguration文档:

http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/cfg/AnnotationConfiguration.html http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/cfg/AnnotationConfiguration.html

Use below : sessionFactory = new Configuration().configure().buildSessionFactory(); 在下面使用:sessionFactory = new Configuration()。configure()。buildSessionFactory();

And also ensure that your cfg.xml should be present in root of src folder. 还要确保cfg.xml应该出现在src文件夹的根目录中。 Else you will get exception of unable to find cfg file 否则,您将获得无法找到cfg文件的异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM