初始sessionFactory创建失败。

发布于 2025-01-24 23:53:36 字数 1323 浏览 2 评论 0原文

我正在学习冬眠,并尝试了一个简单的程序。我使用了Hibernate 5.3版本,并将所有必需的.jar文件添加到构建路径中。我可以在Ref Libraries文件夹中找到配置类,但仍然获得异常。 Hibernate.cfg.xml有什么问题吗?是否必须保存任何特定位置?请帮我。提前致谢。 我正在附上我的项目布局和源代码。

我的hibernateutil.java代码

  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  public class HibernateUtil {
  private static final SessionFactory sessionFactory = 
 buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        System.out.println("hii");
        return new Configuration().configure().buildSessionFactory();
        
    }
    catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." 
 + ex);
        throw new ExceptionInInitializerError(ex);
     }
 }

 public static SessionFactory getSessionFactory() {
     return sessionFactory;
 }

 public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
  }
  }

[项目布局] 项目布局 hibernate confg xml文件

I am learning Hibernate and tried a simple program. I used Hibernate 5.3 version and added all the Hibernate required .jar files to the build path. I can find the Configuration class in the ref libraries folder, still getting the exception. Is there anything wrong with the hibernate.cfg.xml? Is there any specific location it has to be saved? Please help me. Thanks in advance.
I am attaching my project layout and source code.

my HibernateUtil.java code

  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  public class HibernateUtil {
  private static final SessionFactory sessionFactory = 
 buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        System.out.println("hii");
        return new Configuration().configure().buildSessionFactory();
        
    }
    catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." 
 + ex);
        throw new ExceptionInInitializerError(ex);
     }
 }

 public static SessionFactory getSessionFactory() {
     return sessionFactory;
 }

 public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
  }
  }

[Project layout]project layoutHibernate confg xml file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文