且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

org.hibernate.internal.util.config.ConfigurationException:找不到cfg.xml资源[/HibernateTest/src/hibernate.cfg.xml]

更新时间:2023-11-18 18:09:40

如果您的源文件夹的根目录中包含 hibernate.cfg.xml ,请执行

If your hibernate.cfg.xml in the root of the source folder, just do

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

如果它在包中,例如 org.nitish .caller ,用这种方式指定路径

If it is in the package, for an example in the org.nitish.caller, specify path by this way

 SessionFactory sessionFactory = new Configuration()
    .configure("/org/nitish/caller/hibernate.cfg.xml").buildSessionFactory();

您需要关闭会话(在最后块)。不要忘记添加 rollback 代码。

You need to close the session (in the finally block). Don't forget to add rollback code.

请添加 @Table 注释到 UserDetails

更新

Hibernate无法找到 org.postgresql.Driver class的错误原因。它驻留在postgresql jar中。你的图像上有那个jar,但可能是你没有将它添加到类路径中。请参阅如何将JAR添加到项目构建Eclipse中的路径(Java)

The reason of the error that Hibernate can't find org.postgresql.Driver class. It resides in postgresql jar. You have that jar at your image, but may be you don't add it to the classpath. Refer How to Add JARs to Project Build Paths in Eclipse (Java).

关闭中的 session > finally 块,你需要在 try 块之外有 session 变量。

To close a session in the finally block you need to have session variable outside the try block.

    Session session = sessionFactory.openSession();

    try{

    } finally {
        session.close();
   }