gpt4 book ai didi

java.lang.NoSuchFieldError : namingStrategy

转载 作者:行者123 更新时间:2023-12-01 07:03:25 41 4
gpt4 key购买 nike

网上有一个关于如何在 Hibernate 中使用注释的示例(在此之前我也研究过相同的示例,但它使用了 .xml。并且我已经成功地使其正常工作)。所以现在我有:

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

这里有一些代码,可能会有所帮助:

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

最佳答案

嗨,克利奥

您已在 HibernateUtil 类中编写了以下行。

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

用下面的代码替换这行代码并重试。我希望这对您有用。

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

关于java.lang.NoSuchFieldError : namingStrategy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34575603/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com