开发者

Reading persistence.xml from a POJO

开发者 https://www.devze.com 2023-01-14 04:18 出处:网络
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

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?开发者_StackOverflow社区

Please provide your suggestions.


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);
    }
}


This works...

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


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

0

精彩评论

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