昨天解决了一个这样的问题,在打包文件外部读取hibernate的[i]配置文件[/i],也很简单,简而言之就是换一种配置文件的加载方式。
对了,就是File
显而易见,直接读取文件,不做任何路径加载。
如果是发布时,使用环境变量更好。
configuration.configure("D:\\hibernate.cfg.xml");
这样调用会出错,因为配置文件不在WEB-INF下,所以要达到相同的效果,绕过路径加载,用另一个方法:
configuration.configure(new File("D:\\hibernate.cfg.xml"));
对了,就是File
public Configuration configure(File configFile) throws HibernateException {
log.info( "configuring from file: " + configFile.getName() );
try {
return doConfigure( new FileInputStream( configFile ), configFile.toString() );
}
catch (FileNotFoundException fnfe) {
throw new HibernateException( "could not find file: " + configFile, fnfe );
}
}
显而易见,直接读取文件,不做任何路径加载。
如果是发布时,使用环境变量更好。
configuration.configure(new File(System.getenv("XXXX")+"hibernate.cfg.xml"));