开发者

hibernate Annotation Config not finding my xml file

I have some inherited code that uses hibernate. I'm getting the following error:

Caused by: org.hibernate.HibernateException: C:\dev\wk\rs.110-AQU-120.cca.cca-ui\main\config\hibernate\DEV\master.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1402)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1424)
    at com.cca.persistence.HibernateUtil$Parameters.<init>(HibernateUtil开发者_JS百科.java:96)
    at com.cca.persistence.HibernateUtil.<clinit>(HibernateUtil.java:137)
    ... 4 more

This occurs when calling AnnotationConfiguration()#configure(String configurationFile);

But the file does exist. Why would hibernate not be able to locate it?


The path to your ressource must not be a absolute path (C:\dev\wk\rs.110-AQU-120.cca...), it must be the relative path within your application, like the package name!

The reason is, that org.hibernate.util.ConfigHelper.getResourceAsStream load the resorce by ClassLoader.getResourceAsStream(<resourceName>).

For more details, have a look at ClassLoader.getResourceAsStream, and ClassLoader.getResource.


If you want a configuration file at a custom location, you can cast AnnotationConfiguration to Configuration, and provide a File as an argument, for example:

((Configuration) cfg).configure(new File(System.getProperty("user.dir") + "/config/hibernate.cfg.xml"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜