我已经创建了一个 EJB3 项目和一个 JPA 项目。我正在尝试创建一个帮助程序类(它将位于一个单独的项目/jar 中),它将从我的 JPA 项目的 META-INF 文件夹中的 persistance.xml 文件返回持久性单元名称。
如何将此文件作为输入流读取?一旦获得对该文件的引用,我就可以解析这些值,但是如何从另一个 jar 的类中读取一个 jar 的 META-INF 文件夹中的文件?
请提供您的建议。
最佳答案
我在使用持久性单元一段时间后(“突然”)遇到了问题。
以下是有帮助的(一个 b****** 解决方案,但问题仅出现在 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);
}
}
关于java - 从 POJO 读取 persistence.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3639669/