How to load hibernate.cfg.xml from different location

前端 未结 4 1225
说谎
说谎 2021-02-08 08:43

I am creating a jar using hibernate. I have encountered a situation where I need to change a setting (url) often, so I would like to load the hibernate.cfg.xml like

4条回答
  •  抹茶落季
    2021-02-08 09:09

    There is a method public Configuration configure(File configFile) in class Configuration

    Try the following, it should work for sure :)

    File f = new File("D:\\fax\\hibernate.cfg.xml");
    SessionFactory sessionFactory = new Configuration().configure(f).buildSessionFactory();
    

    The difference is you have used a method configure(String resource) which is expecting a resource in a classpath, but where as configure(File configFile) is expecting a File, so you can pass it.

提交回复
热议问题