【发布时间】:2015-06-22 11:58:31
【问题描述】:
我有一个struts2 和hibernate 应用程序。我打算做的是,如果数据库配置无效,我从用户那里收到它并使用更新的设置修改 hibernate.hbm.xml。我能够检查和接收用户的设置,唯一的问题是 domparser 找不到我的 hibernate.cfg.xml。
String filepath = "hibernate.cfg.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
当以普通用户身份部署应用程序时,我得到了这个
/home/uzumaki/hibernate.cfg.xml (No such file or directory)
当我以 root 身份运行时,
/hibernate.cfg.xml (No such file or directory)
使用 File("").getAbsolutePath()) 并与 hibernate.cfg.xml 连接也会给我同样的错误。
该文件当然不存在,它与我的 java 代码/类在同一个文件夹中,并且随处可见(示例 war 文件)
【问题讨论】:
-
使用
this.getResource("hibernate.cfg.xml")。在我看来,允许在运行时更改休眠文件看起来并不安全。 -
@Priyesh、wordpress、phpbb 等,无论其平台如何,都自行编写数据表配置。我只想做同样的事情,删除硬编码。
标签: java hibernate jsp servlets domparser