org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/HibernateTest/src/hibernate.cfg.xml]

前端 未结 6 702
故里飘歌
故里飘歌 2020-12-28 11:50

I am trying to connect to Postgresql9.1 in ubuntu with pgadmin3. My Pgadmin3 GUI tool does not give any option to create tables by right clicking the database, but it is ava

相关标签:
6条回答
  • 2020-12-28 11:50

    All the previous answers are perfect. However, if u need a really quick solution then I would recommend you to just put your hibernate.cfg.xml file in your source folder and write

    SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
    

    This is simple and works!

    0 讨论(0)
  • 2020-12-28 11:52
    new Configuration().configure(new File("hibernate.cfg.xml")).buildSessionFactory());
    

    This solved the error for me!

    0 讨论(0)
  • 2020-12-28 11:55

    new Configuration().configure() takes hibernate.cfg.xml from root of class path directory. new Configuration().configure("/com/company/project/hibernate.cfg.xml") takes from root of class path + com/company/project/hibernate.cfg.xml.

    If you are using different file name for hibernate configuration from root of class path, then eg: new Configuration().configure("/database.cfg.xml")

    If you want to give the full system path of the configuration file then new Configuration().configure(new File("/home/visruth/config/hibernate.cfg.xml)) which takes the configuration file from the given exact location.

    0 讨论(0)
  • 2020-12-28 12:01

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

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

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

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

    Please, add @Table annotation to the UserDetails.

    Update

    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).

    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();
       }
    
    0 讨论(0)
  • 2020-12-28 12:08

    You have to use the absolute path of the file otherwise this will not work. Then with that path we build the file and pass it to the configuration.

    private fun getFile(): File {
        val currentWorkingDir = System.getProperty("user.dir")
        val absoulutePath = "$currentWorkingDir/src/main/resources/secret-hibernate.cfg.xml"
        println("Absolute Path of secret-hibernate.cfg.xml: $absoulutePath")
        return File(absoulutePath)
    }
    

    https://stackoverflow.com/a/64084771/5279996

    GL

    0 讨论(0)
  • 2020-12-28 12:09

    I fixed this by moving my config file to src/main/resources. This is the standard directory for configuration files like hibernate.cfg.xml or hibernate.properties or application related properties files.

    0 讨论(0)
提交回复
热议问题