春天 + Hibernate 会话工厂:映射 JAR 内的文件目录

发布于 2024-12-17 20:34:02 字数 771 浏览 0 评论 0原文

我有基于 Hibernate 和 Spring 的 Web 项目(编程配置)。 Hibernate 映射在一个包中提供,该包存档在 JAR 中。

当谈到初始化会话工厂时,我曾经调用:

sessionFactory.
    setMappingDirectoryLocations(new Resource[]{
        new ClassPathResource("org/all/theway/to/hibernatemappings")});

为了告诉 Hibernate 在哪里寻找映射文件。 “org/all/theway/to/hibernatemappings”是包含 hbm.xml 文件的包。这在 Eclipse(GWT 开发模式)中运行良好,因为包含映射的项目也被签出并链接到我的 Web 项目。但是,一旦我创建了一个war并将其部署到Tomcat,它就无法获取类路径资源。

:“如果类路径资源驻留在文件系统中,则支持解析为 java.io.File,但不支持 JAR 中的资源。始终支持解析为 URL。

Spring 的 ClasspathResource javadoc 暗示了这一点 该怎么办?我也可以使用 setMappingJarLocation 代替,但我不喜欢在 Spring 上下文中硬编码 jar 文件名。此外,当我尝试它时,它也只能在IDE中工作,但在Tomcat中相同的文件路径(WEB-INF/lib/file.jar)不起作用。这也让我相信这将是一个丑陋的解决方案。

是否有一种无需使用 jar 文件即可工作的解决方法?

I have web project based on Hibernate and Spring (programmatic configuration). The Hibernate mappings are provided in a package which is archived in a JAR.

When it comes to initialize the session factory I used to call:

sessionFactory.
    setMappingDirectoryLocations(new Resource[]{
        new ClassPathResource("org/all/theway/to/hibernatemappings")});

in order to tell Hibernate where to look for mapping files. "org/all/theway/to/hibernatemappings" is the package which contains the hbm.xml files. This worked fine within Eclipse (GWT dev mode), as the mapping-containing project is also checked out and linked to my web project. However, once I create a war and deploy it to Tomcat, it fails to get the class path resource.

Spring's ClasspathResource javadoc implies this: "Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL. "

But what to do instead? I could also use setMappingJarLocation instead, but I do not like to hardcode a jar file name in my Spring context. Further, when I tried it, it also only worked within IDE, but inside Tomcat the same file path (WEB-INF/lib/file.jar) did not work. This also makes me believe that this would be an ugly solution.

Is there a workaround which works without using the jar file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴梦一场 2024-12-24 20:34:02

这有效

//所有应用程序上下文实现 ResourcePatternResolver

ResourcePatternResolver resourcePatternResolver= applicationContext;
sessionFactory.setMappingDirectoryLocations(resourcePatternResolver.getResources("classpath*:org/all/theway/to/hibernatemappings/*.hbm.xml"));

编辑:用 ResourcePatternResolver 替换 DefaultResourceLoader。

This works

//All application contexts implements ResourcePatternResolver

ResourcePatternResolver resourcePatternResolver= applicationContext;
sessionFactory.setMappingDirectoryLocations(resourcePatternResolver.getResources("classpath*:org/all/theway/to/hibernatemappings/*.hbm.xml"));

EDIT: Replaced DefaultResourceLoader with ResourcePatternResolver.

音盲 2024-12-24 20:34:02

这并不是原始问题的真正解决方案,而是避免在配置代码中处理 jar 文件的文件系统文件名的解决方法:

我只是告诉 ant 将有问题的 JAR 解压到 WEB-INF/classes 文件夹中

<target name="unpack.hibernatemappings">
    <unzip dest="${war.dir}/WEB-INF/classes">
        <fileset dir="${lib.dir}">
            <include name="archive-containing-the-hbm-files.jar"/>
        </fileset>
    </unzip>
</target>

现在,Hibernate 映射驻留在其中在文件系统中,可以作为 ClassPathResource 进行访问(如问题的示例中所示)。

Not really a solution of the original problem but a workarounnd which avoids dealing with filesystem file names of jar files within the configuration code:

I simply told ant to unpack the JAR in question into the WEB-INF/classes folder

<target name="unpack.hibernatemappings">
    <unzip dest="${war.dir}/WEB-INF/classes">
        <fileset dir="${lib.dir}">
            <include name="archive-containing-the-hbm-files.jar"/>
        </fileset>
    </unzip>
</target>

Now, the Hibernate mappings reside in the file system and can be accessed as ClassPathResource (like in the questions's example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文