初始SessionFactory创建failed.org.hibernate.HibernateException:找不到/hibernate.cfg.xml 线程" main"中的例外情况java.lang.ExceptionInInitializerError
at training.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
at training.HibernateUtil.<clinit>(HibernateUtil.java:8)
at training.HibernateMain.main(HibernateMain.java:15)
引起:org.hibernate.HibernateException:找不到/hibernate.cfg.xml
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2149)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2130)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2110)
at training.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 2 more
答案 0 :(得分:0)
您的代码是正确的,但由于数据库连接,会话工厂无法创建
因此,请在项目的META_INF文件夹中添加hibernate.cfg.xml文件。
1) add your Room class in the cfg file
2) add properties to connect the database like connection url, username, password and database driver class in your cfg file.
如果项目中没有META_INF文件夹,则直接放入src文件夹。
试试吧。