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

前端 未结 6 720
故里飘歌
故里飘歌 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 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();
       }
    

提交回复
热议问题