从 POJO 读取 persistence.xml

发布于 2024-09-18 08:55:11 字数 229 浏览 3 评论 0原文

我创建了一个 EJB3 项目和一个 JPA 项目。我正在尝试创建一个帮助程序类(它将位于一个单独的项目/jar 中),它将从位于我的 JPA 项目的 META-INF 文件夹中的 persistance.xml 文件返回持久性单元名称。

如何将此文件作为输入流读取?一旦获得对此文件的引用,我就可以解析这些值,但是如何从另一个 jar 的类中读取 jar 的 META-INF 文件夹中的文件?

请提供您的建议。

I have created an EJB3 project and a JPA project. I'm trying to create a helper class(which will be in a separate project/jar) that will return the Persistence Unit Name from the persistance.xml file which is in the META-INF folder of my JPA project.

How can I read this file as an Input Stream? I can parse the values once I get a reference to this file, but how do I read the files in META-INF folder of a jar from a class of another jar?

Please provide your suggestions.

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

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

发布评论

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

评论(3

你在看孤独的风景 2024-09-25 08:55:11

我在使用持久性单元一段时间后遇到了问题(“突然”)。

这是有帮助的(ab******-解决方案,但问题仅出现在 devMode 中,并通过此解决):

    try {
        InputStream is = Thread.currentThread().getContextClassLoader(
                ).getResource("META-INF/persistence.xml").openStream();
        FileOutputStream output= new FileOutputStream("c:/tmp/show.xml");
        IOUtils.copy(is, output);
        IOUtils.closeQuietly(output);
        PersistenceUnitInfoImpl impl= new PersistenceUnitInfoImpl();
        impl.setPersistenceXmlFileUrl(new URL("file://c:/tmp/show.xml"));
        JpaBasics basics= new JpaBasics("spektrum");
        basics.init(false);
        EntityManager em= basics.getEm();
        _log.debug("have we an em: " + em);
        new TestLoad().testLoadMagazines();
    } catch (Exception e) {
        _log.error("", e);
    }
}

I had the problem after a time using the persistence-unit ("suddenly").

here's what helped (a b******-solution, but the problem occurs only in devMode and is solved with this):

    try {
        InputStream is = Thread.currentThread().getContextClassLoader(
                ).getResource("META-INF/persistence.xml").openStream();
        FileOutputStream output= new FileOutputStream("c:/tmp/show.xml");
        IOUtils.copy(is, output);
        IOUtils.closeQuietly(output);
        PersistenceUnitInfoImpl impl= new PersistenceUnitInfoImpl();
        impl.setPersistenceXmlFileUrl(new URL("file://c:/tmp/show.xml"));
        JpaBasics basics= new JpaBasics("spektrum");
        basics.init(false);
        EntityManager em= basics.getEm();
        _log.debug("have we an em: " + em);
        new TestLoad().testLoadMagazines();
    } catch (Exception e) {
        _log.error("", e);
    }
}
她如夕阳 2024-09-25 08:55:11

这有效...

InputStream is = Thread.currentThread().getContextClassLoader().getResource("META-INF/persistence.xml").openStream();

This works...

InputStream is = Thread.currentThread().getContextClassLoader().getResource("META-INF/persistence.xml").openStream();
败给现实 2024-09-25 08:55:11

如果您的其他 jar 位于类路径中,您应该能够使用以下命令加载此文件:
getClass().getResourceAsStream("META-INF/persistence.xml");

If your other jar is in the classpath, you should be able to load this file using:
getClass().getResourceAsStream("META-INF/persistence.xml");

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